Archive for the ‘scheme’ tag
Scheme foldr and pop
Pop:
(define ( pop lst) (cond ((> (length lst) 1 ) (cons (car lst) (pop (cdr lst)))) (else '())))
foldr:
(define (foldr op ini lst) (cond ((> (length lst) 1 ) (op (car lst) (foldr op ini (cdr lst)))) (else (op (car lst) ini ))))
Coming soon in prolog.
Union in Scheme vs Prolog
Scheme :
(define (union x y)
(define (append l1 l2)
(cond
((null? l1) l2)
(else
(cons (car l1) (append (cdr l1) l2)))))
(define (prune l)
(cond
((null? l) '())
((member (car l) (cdr l))
(prune(cdr l)))
(else
(cons (car l) (prune (cdr l))))))
(prune (append x y)) )
*Just the length of this makes me think there’s prolly a better way to do this ? (Other than using the built-in append)
Prolog:
isunion([],Same,Same).
isunion([First|Rest], Second, UCand):-
member(First, Second),!, isunion(Rest,Second,UCand).
isunion([First|Rest],Second,[First|UCand]):- isunion(Rest, Second, UCand).
Damn.
A little scheme
trying out scheme too.
The scheme version of the max in a list function :
(define (max-num a)
(cond ((null? (cdr a)) (car a))
((> (car a) (car(cdr a)))
(max-num (cons (car a) (cdr(cdr a)))))
(else (max-num (cdr a)))))
At this point, I like scheme better.
P.S: The book little schemer is the way to go for a quick and painless intro.