Isolate two parts of a program
Interface between programs and data definition.
Data objects constructed from Pair (compound-data primitive)
(define (make-rat n d)
(let ((g ((if (< d 0) - +) (gcd n d))))
(cons (/ n g) (/ d g))))
(define (numer x) (car x))
(define (denom x) (cdr x))
(define (add-rat x y)
(make-rat (+ (* (numer x) (denom y))
(* (numer y) (denom x)))
(* (denom x) (denom y))))
(define (print-rat x)
(newline)
(display (numer x))
(display "/")
(display (denom x)))
Rational Number Example:
Procedures must meet the requirement below
(numer x) n
--------- = ---
(denom x) d
Condition above rely on representation of integer
Exercise 2.4 - Alternative Implementation of Cons, Car, Cdr use Applicative order
Exercise 2.5 - Represent non-negative int using only number and arithmetic operations
Exercise 2.6 - Define numbers and addition without numbers Church Numeral
List
cons
(list 1 2 3 4)
equals to (cons 1 (cons 2 (cons 3 (cons 4 nil))))
access an element
(define (list-ref items n)
(if (= n 0)
(car items)
(list-ref (cdr items) (- n 1))))
(define squares (list 1 4 9 16 25))
(list-ref squares 3)
16
get a list’s length
(define (length items)
(define (length-iter a count)
(if (null? a)
count
(length-iter (cdr a) (+ 1 count))))
(length-iter items 0))
append
(define (append list1 list2)
(if (null? list1)
list2
(cons (car list1) (append (cdr list1) list2))))