Nicht angemeldet. · Kennwort vergessen · Registrieren

Nora Go
Mitglied seit 10/2014
13 Beiträge
Betreff: Prolog takeout function - error in solution
Just wanted to point out a very small error :) maybe this helps somebody understand Prolog better:

"Start with a helper predicate takeout(X,LSA,LSB) that is true iff LSB is the result
of removing the first occurence of X from LSA."

Given solution:

takeout(X,[H|T1],[H|T2]) :- takeout(X,T1,T2).


takeout(X,[H|T1],[H|T2]) :- X\=H, takeout(X,T1,T2).

Try both with "takeout(1, [1,2,3,1], X)." and don't forget to click on next.
Mitglied seit 12/2017
62 Beiträge
Alternative (EDIT: in this context false, see comment below!) correction would be the following code; it's often easier to write stuff down like this, especially when doing a search:

takeout(X,[H|T1],[H|T2]) :- takeout(X,T1,T2).

The ! tells Prolog to stop all backtracking after its occurrence. Because Prolog goes from top to bottom it is correct to stop when we found X because we have our one and only solution.
Dieser Beitrag wurde 2 mal verändert, zuletzt am 10.02.2019, 17:43 von LasagneAlForno.
Mitglied seit 05/2014
46 Beiträge
+1 LasagneAlForno
Zitat von LasagneAlForno:
takeout(X,[H|T1],[H|T2]) :- takeout(X,T1,T2).

The problem with this soultion however is, that it also stops the permutations from backtracking and you therefore only get 1 result
Schließen Kleiner – Größer + Auf diesen Beitrag antworten:
Prüfcode: VeriCode Gib bitte das Wort aus dem Bild ins folgende Textfeld ein. (Nur die Buchstaben eingeben, Kleinschreibung ist in Ordnung.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Weitere Zeichen:
Gehe zu Forum
Datenschutz | Kontakt
Powered by the Unclassified NewsBoard software, 20150713-dev, © 2003-2011 by Yves Goergen