Advertisement
logicmoo

Untitled

Dec 11th, 2014
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 0.65 KB | None | 0 0
  1. Scheme:
  2.  
  3. (define (pickNumToAdd M N)
  4.   (if (null? M) 0
  5.     (+ (nth N (car M)) (pickNumToAdd (cdr M) N))))
  6.  
  7.  
  8. Prolog:
  9.  
  10. pickNumToAdd(M,N,Out) :-  
  11.   M==[] ->  
  12.      Out = 0
  13.     ;
  14.    (
  15.      [CAR|CDR] = M, nth(N,CAR,NTH),
  16.        pickNumToAdd(CDR,N,MID),
  17.           Out is NTH + MID
  18.    ).
  19.  
  20.  
  21. Frontal:
  22.  
  23. f_nth(A,[A|B],1).
  24. f_nth(ListItemValue,[A|B],N) :- N>0, N1 is N-1, f_nth(ListItemValue,B,N1).
  25.  
  26. f_car(CAR,[CAR|_]).  
  27. f_cdr(CDR,[_|CDR]).
  28.  
  29. f_pickNumToAdd(Out,M,N) :-  
  30.   M==[] ->  
  31.      Out = 0
  32.     ;
  33.    (
  34.      f_car(CAR,M), f_nth(NTH, N, CAR),
  35.        f_cdr(CDR,M),
  36.        f_pickNumToAdd(MID,CDR,N),
  37.           Out is NTH + MID
  38.    ).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement