Not logged in. · Lost password · Register

Nora Go
Member since Oct 2014
18 posts
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,[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.
Member since Dec 2017
76 posts
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.
This post was edited 2 times, last on 2019-02-10, 16:43 by LasagneAlForno.
Member since May 2014
48 posts
+1 LasagneAlForno
Quote by 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
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Go to forum
Datenschutz | Kontakt
Powered by the Unclassified NewsBoard software, 20150713-dev, © 2003-2011 by Yves Goergen