#lang racket ; Integers: (quotient 9 4) (remainder 9 4) (modulo 9 5) (modulo 9 -4) (modulo -9 5) (modulo -9 -4) (= 2 3) (not (= 2 3)) (< 3 4) ; Lists: (define L '(1 2 3)) (first L) (car L) (rest L) (cdr L) (first (rest L)) (car (cdr L)) (cadr L) (cons (first L) (rest L)) (cons 42 L) (cons L L) (cons 1 1) (cons 1 (cons 2 (cons 3 '()))) (cons (cons (cons (cons 1 '()) '()) '()) '()) (list? L) (null? L) (null? (rest '(2))) (list? 1) (null? 1) (define LL '( (1 2 3) (4 5 6 (7 8) ))) (car (car LL)) (caar LL) (car (cdr LL)) (cadr LL) (car (cdr (cdr LL))) (caddr LL) (cdr (cdr (car (cdr LL)))) (cddadr LL) (car (cdr (cdr (car (cdr LL))))) (caddadr LL) ; If you want the list consisting of the value of a and the value of b, then you ; need to assemble it. So you start with the empty list '() ; (this is a constant, so has a quote). Then you use the inductive definition ; of list, and build the list containing b, by doing (cons b '()) ; finally you add a to the front, by doing (cons a (cons b '())) ; Definition and Quotation: (quote 1) '1 (quote ()) '() (define X (+ 1 2)) X (define Y '(+ 1 2)) Y (eval Y) ; Abstraction: ; the constant function of no arguments (define f1 (lambda () 42)) f1 (f1) ; the unary function that doubles its argument (define f2 (lambda (x) (+ x x))) (f2 3) ; the identity function (define ident (lambda (x) x)) (ident 42) ; the function that builds a list around its argument (define list1 (lambda (x) (cons x '()) ) ) (list1 3) ; a binary function that does addition (define add (lambda (x y) (+ x y))) (add 40 2) ; Scheme doesn't do Currying automatically (add 40) ; (addn n) builds a function that adds n to it's argument (define addn (lambda (n) (lambda (x) (+ x n)))) addn (addn 3) (define plus3 (addn 3)) plus3 (plus3 40) ; Conditionals and Recursion: ; in an imperative language, we would do something like this to ; find the min of a list ; def lmin(L): ; min_val = None ; for x in L: ; if min_val is None or x < min_val: ; min_val = x ; return min_val ; functionally, we do it like this ; try tracing it in DrRacket (define lmin (lambda (L) (if (null? (rest L)) (first L) (min (first L) (lmin (rest L))) ))) (lmin '(1 -3 4)) ; sum over a list: note the base case, and induction/recursion (define lsum (lambda (L) (if (null? L) 0 (+ (first L) (lsum (rest L))) ))) (lsum '(1 -3 4)) ; Evaluation (quote (+ 3 4)) (eval (quote (+ 3 4)) (define exp (cons (lambda (x) (+ x x)) '(3))) exp (eval exp)