[net.ai] HELP!!!!!

booter@lll-crg.ARpA (Elaine Richards) (05/14/86)

I have been given this silly assignment to do in Prolog, a language which
is rapidly losing my favor. We are to do the folowing:

Define a Prolog predicate len(N,L) such that N is the length of list L.

Define a Prolog predicate nth(X,N,L) such that X is the Nth element of
list L.

I cannot seem to instantiate N past the value 0 on any of these.

My code looks like this:

len(N,[]) :- 0 !.
len(N,[_|Y] :- N! is N + 1,len(N1,L].

It gives me an error message indicating "_6543etc" or somesuch ghastly 
number/variable refuses to take the arithmetic operation.

The code for nth is similar and gives a similar error message.

Please send replies to {whateverthepathis}lll-crg!csuh!booter.

E
*****

mozetic@uiucdcsb.CS.UIUC.EDU (05/16/86)

% How about the following:

len( 0, [] ).
len( N, [_ | L] ) :- len( N0, L ), N is N0 + 1.

nth( X, 1, [X | _] ).
nth( X, N, [_ | L] ) :- N > 1, N0 is N - 1, nth( X, N0, L ).