; lispcode.s

; (load "lispcode.s")


(define (factorial integer)
                (if (= 0 integer) 1
                          (* integer (factorial (-1+ integer)))))

(display (factorial 7))  (newline)

;fact from p.390 Sethi
(define (fact n)
	(if (= n 0)
	1
	(* n (fact (- n 1)))))

(display (fact 7))       (newline)

(define (square x) (* x x))

(display (square 7))     (newline)

((lambda (x) (* x x)) 7)

(writeln (car '(A B C)))
(writeln (cdr '(A B C)))

(writeln (zero? 0))
(writeln (zero? 1))

(writeln (number? 0))
(writeln (number? '(A B C)))

(writeln (cons 'first (cons 'second (cons 'third nil))))

(define (our-length lis)
        (if (null? lis) 0 (+ 1 (our-length (cdr lis)))))

(writeln (our-length '(A B C)))

; let construct from p. 391 Sethi
(let ((three-sq (square 3)))
	(+ three-sq three-sq) )

(writeln (null? nil))
(writeln (null? ()))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Functions for list manipulation
; Scheme versions from p.397 Sethi

(define (rev x z)
        (cond ((null? x) z)
              (else (rev (cdr x) (cons (car x) z)))))
(define (our-reverse L) (rev L ()))

(writeln (our-reverse '(A B C)))


(define (our-append x z)
        (cond ((null? x) z)
              (t (cons (car x) (our-append (cdr x) z)))))


(writeln (our-append '(1 2 3) '(4 5 6)))

(define (our-map f x)
        (cond ((null? x) x)
              (else (cons (f (car x)) (our-map f (cdr x))))))

(writeln (fact 7))

(writeln (mapcar square '(1 2 3 4 5)))

(writeln (mapcar fact '(1 2 3 4 5)))

(writeln (our-map fact '(1 2 3 4 5)))

(define (our-remove-if f x)
        (cond ((null? x) '())
              ((f (car x)) (our-remove-if f (cdr x)))
              (else (cons (car x) (our-remove-if f (cdr x))))))

(define (positive x) (> x 0))

(writeln (our-remove-if positive '(0 1 -1 2 -2 3 -3)))

(define (our-reduce f x v)
        (cond ((null? x) v)
              (else (f (car x) (our-reduce f (cdr x) v)))))

(define (add x y) (+ x y))

(writeln (our-reduce add '(1 2 3 4 5) 0))


;;;;;;;;;;;;;;;;;;;;;;;;;

(define (map2 f x y)
        (cond ((null? x) '())
              (else (cons (f (car x) (car y)) (map2 f (cdr x) (cdr y))))))

(writeln (map2 list '(a b c) '(1 2 3)))


;;;;;;;;;;;;;;;;;;;;;;;;;;


; io.s

(define output (open-output-file "a:/scode/shw.out"))

(write 'Test output) (newline output)
(write 'Test) (newline)

(write (car '(a b c)) output) (newline output)
(write (car '(a b c))) (newline)

(close-output-port output)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Self

((lambda (x) (list x (list (quote quote) x))) (quote (lambda (x) (list x (list (quote quote) x)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; eq?

(define a (cons 3 '()))
(define b (cons 3 '()))
(eq? a a)
(eq? a b)
(eq? (cons 1 '(2)) (cons 1 '(2)))
(eq? '() '())
(define c b)
(eq? b c)



