Everything's Beta

things I don't get to do at work :)

Archive for the ‘scheme’ tag

Scheme foldr and pop

without comments

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.

Written by srijak

November 3rd, 2008 at 4:17 am

Posted in code

Tagged with

Union in Scheme vs Prolog

without comments

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.

Written by srijak

November 2nd, 2008 at 8:18 am

Posted in code

Tagged with ,

A little scheme

without comments

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.

Written by srijak

November 2nd, 2008 at 8:07 am

Posted in code

Tagged with