Not logged in. · Lost password · Register

 Member since Oct 2014 18 posts 2019-02-10, 15:26   #1   Subject: 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 Member since Dec 2017 76 posts 2019-02-10, 15: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. This post was edited 2 times, last on 2019-02-10, 16:43 by LasagneAlForno.
 Member since May 2014 48 posts 2019-02-10, 15:47   #3   +1 LasagneAlForno Quote by 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
Close Smaller – Larger + Reply to this post:
Verification code: Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys:
Special characters:
Go to forum
Datenschutz | Kontakt