;; A quick introduction to Scheme 
;; Tested using GNU/MIT Scheme 


;; Basic evaluation 
;; a) Constant atoms evaluate to themselves
;; b) Identifiers looked in current environment 
;; c) First expression of a list must evaluate
;;    to a function. The function is applied to 
;;    the evaluated values of the rest of the list. 

;; Simple arithmetic expressions 
(+ 3 (* 4 5))

;; calling a function 
(gcd 10 35) 

;; preventing the evaluation of a list 
;; (2.1 2.2 3.1)
;; Error 2.1 is not a function

'(2.1 2.2 3.1)
(quote (2.1 2.2 3.1))


;; Binding 
(define a 5)

;; function definition and 
;; conditional expression - special form - delayed evaluation 

(define (gcd u v) 
  (cond ((= v 0) u)
	((< v 0) 0)
	(else (gcd v (remainder u v)))))


;; All data structures are lists no type checking 

(define myl '(1 2 3))

;; Head of list 
;; contents of address register 
(car myl) 

;; Tail of list 
;; contents of decrement register 
(cdr myl)  

;; construct a list from a head and a tail 
(cons 3 '(1 2 3))

;; append function 

(define (append L M)  
  (if (null? L) M 
      (cons (car L) (append (cdr L) M))))

(append '(1 2) '(3 4))



;; Higher order function 

(define (mymap f L) 
  (cond ((null? L) '())
	(else (cons (f (car L)) (mymap f (cdr L))))))

(define (sqr x) (* x x))
(mymap sqr '(1 2 3 4))


;; Anonymous functions like the fn notation in SML 

(define square (lambda (x) (* x x)))
(map (lambda (x) (* x x)) '(1 2 3 4))


;; Let declarations 

(define (foo x) 
  (let ((a 5))
    (+ x a)))


