Learning Scheme.

So I started learning Scheme today. Amazing language. I am reading little schemer book. The best part is more than a language I learnt a lot about lambda calculus.

I have always loved functional programming language but using scheme has convinced me to think about it in a different and broad way. Working with Lisp(Scheme) is so easy. You can basically write your own language or atleast set of features in no time. Also you have more control.

I started implementing basic stuff in scheme. I thought of sharing some code. I don't think its the perfect implementation but I guess its good for the start.

I have implemented each, map, reduce and filter functions in scheme.

Here is the code:

(define each
    (lambda (lat proc)
        (cond
         ((null? lat))
         (else
            (proc (car lat))
            (each (cdr lat) proc)))))

(define map
    (lambda (lat proc)
        (cond
         ((null? lat) ())
         (else
             (cons
                (proc (car lat))
                (map (cdr lat) proc)
                )
            )
         )
        )
    )

(define reduce
    (lambda (lat proc init) 
        (cond
         ((null? lat) (proc 0 init))
         (else
            (reduce
             (cdr lat) proc (proc (car lat) init))))))

(define filter
    (lambda (lat proc) 
        (cond
         ((null? lat) ())
         ((proc (car lat))(cons (car lat) (filter (cdr lat) proc)))
         (else
            (filter (cdr lat) proc)))))
Share on : Twitter, Facebook or Google+