I’m trying to implement a Scheme-like map function, i.e.
map([X1, X2, ..], Fun) ->[Fun(X1), Fun(X2), ...]
I wrote this code:
map([], Fun, []).
map([H|T], Fun, [HO|TO]) :- call(Fun, H, HO), map(T,F,TO).
Now, looking at this run:
?- map([1,2,3], plus(1), X).
X = [2, 3, 4] ;
X = [2, 3, 4] ;
X = [2, 3, 4] ;
X = [2, 3, 4] ;
ERROR: map/3: Arguments are not sufficiently instantiated
Exception: (9) map([3], _G380, _G351) ?
- How can I make it stop after the first solution provided?
- How can I trace it?If i use
traceit stop the execution after the first solution.
1) the code you posted has a problem:
map(T,F,T0)should bemap(T,Fun,T0). without this change i get the error you mentioned immediately; with this correction it runs flawlessly. (it is also a good idea to changemap([], Fun, [])tomap([],_Fun,[])since you dont use the variableFun– you should get a warning for singleton variables)2) when you trace it and reach the first solution, press
;. alternatively, in swi-prolog, press thespacebarinstead ofenterand the tracing will continue.