0
votes

Basically what I'm trying to do here is make a predicate "first" which takes first C members from a list and makes a new list out of them in the same arrangement. e.g. first([a,b,c,d,e],3,R). would return R = [a,b,c]. Now I'm getting an infinite loop and cannot find the cause of that. Thanks for your help.

first([],_,[]).
firts([_], 0, []).
first([H|T], C, R):- C >= 1,
                 append(R, [H], R2),
                 C2 is C - 1,
                 first(T, C2, R2).
1

1 Answers

0
votes

Found the answer in another thread.

take_n(N, List, Taken) :-
    ( length(List, M),
      N > M
    ->
      Taken = List
    ;
      length(Taken, N),
      append(Taken, _, List)
    ).