The assignment:
"Write subst(A,B,C,D) where list D is list C with element A substituted for all occurrences of element B."
The first case we want to handle is the base case. That is, if we have the empty list, it does not matter what A and B are, because there is nothing to replace. Therefore:
subst(A, B, [ ], [ ]).
Or, because we are using neither A nor B, we can write:
subst(_, _, [ ], [ ]).
The alternative is that we are not dealing with an empty list. In such a situation, we want to define subst recursively, such that we yank out the first element -- the head -- if it is B, and replace it with A. Unless of course the head is not element B, in which case we want to just leave it alone -- that is, put the head back on the list.
The way we pull apart such a list is as [H | T], where H is the head and T is the tail.
More on this later, but hopefully this gets you on the right track. It does not have to work exactly.
Monday, May 26, 2008
subst in Prolog, pt i
Posted by joshwaxman at 2:43 PM 0 comments
Wednesday, May 14, 2008
8, 9, 10, 13, 15, 16
ch 8 -- control structures p 324 and on
if-fi
do
od
ch 9, ch 10: parameter passing
skip 380-387
focus on coroutines, on page 391
in ch 10, statis links, dynamic links
skip ch 11, 12, 14
do chapter 13
Posted by joshwaxman at 2:41 PM 0 comments
Monday, May 12, 2008
Common Lisp Tutorial
http://www.notam02.no/internt/cm-sys/cm-2.2/doc/clt.html
Posted by joshwaxman at 12:55 PM 0 comments
Thursday, May 08, 2008
Not Equal In Prolog
After consulting with this website, actually connected with pop11. :)
The following interaction from SWI-Prolog when consulting j.pl. Use similar code to make sure sally is not her own sibling.
% c:/j.pl compiled 0.00 sec, -292 bytes
11 ?- listing.
myneq(A, B) :-
A\=B.
Yes
12 ?- myneq(a, a).
No
13 ?- myneq(a, b).
Yes
Posted by joshwaxman at 2:24 PM 0 comments
Labels: prolog