Nicht angemeldet. · Kennwort vergessen · Registrieren

 Mitglied seit 10/2014 13 Beiträge 10.02.2019, 16:26   #1   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,[X|T],T). takeout(X,[H|T1],[H|T2]) :- takeout(X,T1,T2). Correction: takeout(X,[X|T],T). 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.
 LasagneAlForno Mitglied seit 12/2017 62 Beiträge 10.02.2019, 16:34   #2   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,[X|T],T):-!. 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 10.02.2019, 16:47   #3   +1 LasagneAlForno Zitat von LasagneAlForno:takeout(X,[X|T],T):-!. 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: Gib bitte das Wort aus dem Bild ins folgende Textfeld ein. (Nur die Buchstaben eingeben, Kleinschreibung ist in Ordnung.)
Smileys:
Weitere Zeichen:
Gehe zu Forum
Datenschutz | Kontakt