NUIM/CS351 Lecture 02 Notes

Module: CS351 (Programming Paradigms)
Lecturer: Barak Pearlmutter
Lecture No: 2
;;; Scheme has:

;;;  - everything is an expression (no "statements")
;;;  - paren means function call (with limited exceptions)
;;;  - exceptions: DEFINE, LAMBDA, IF
;;;  - variables are not "typed"

;;; Values

;;;  - numbers (exactness, rationals, complexes, typical numeric "basis")
;;;  - booleans, #F = unique "false" value, #T or anything else aside from #f = true
;;;  - lists, can contain ANY element including lists, can be EMPTY LIST, CONS, CAR, CDR
;;;  - (QUOTE X) => X, syntactic sugar (QUOTE X) = 'X

;;; define IOTA
;;;  (IOTA 10) => (0 1 ... 9)

(define iota
  (lambda (n)
    (reverse (riota (- n 1)))))

;;; (riota 10) => (10 9 8 ... 0)
;;; (riota 10) = (cons 10 (riota 9))
;;; (riota n) = (cons n (riota (- n 1)))
;;; (riota 2) = (cons 2 (riota 1))
;;; (riota 1) = (cons 1 (riota 0))
;;; (riota 0) = '(0) = (cons 0 (riota -1))
;;; (riota -1) = EMPTY LIST '()

(define riota
  (lambda (n)
    (if (= n -1)
        (list)
        (cons n (riota (- n 1))))))