griffin(52)% rs RScheme (v0.7.2, 97.12.21) Copyright (C) 1995-1997 Donovan Kolbly top[0]=>(append '(1 2 3) '(4 5 6)) value := (1 2 3 4 5 6) top[3]=>(define x '(1 2 3)) value := x top[4]=>(define y '(4 5 6)) value := y top[5]=>(append x y) ;; append does not modify its parameters value := (1 2 3 4 5 6) top[6]=>x value := (1 2 3) top[7]=>y value := (4 5 6) top[9]=>(define (append l1 l2) (cond ((null? l1) l2) (else (cons (car l1) (append (cdr l1) l2))) )) top[10]=>(append '(1 2 3) '(5 6 7)) value := (1 2 3 5 6 7) top[11]=> top[11]=>(reverse '(1 2 3 4)) value := (4 3 2 1) top[12]=>(reverse '(1 2 (3 4) 5)) value := (5 (3 4) 2 1) ;;; this is the straightforward, but O(n^2), version of reverse top[13]=>(define (rev l) (cond ((null? l) '()) (else (append (rev (cdr l)) (list (car l)))) )) value := rev top[14]=>(rev '(1 2 (3 4) 5)) value := (5 (3 4) 2 1) top[15]=> top[15]=> ;; here is the O(n) version (define (newrev l accum) (cond ((null? l) accum) (else (newrev (cdr l) (cons (car l) accum))) )) break[0]=>(newrev '(1 2 (3 4) 5) '()) value := (5 (3 4) 2 1) break[1]=>(define (rev2 l) (newrev l '())) break[2]=>(rev2 '(1 2 (3 4) 5)) value := (5 (3 4) 2 1) top[0]=> ;; assignment using set! top[0]=>(define x 7) value := x top[1]=>x value := 7 top[2]=>(set! x (- x 1)) value := 6 top[3]=>x value := 6 top[4]=>;; for modifying the car and cdr fields of a cons cell top[4]=>;; use set-car! and set-cdr! top[4]=>(define l '(1 2 3)) value := l top[5]=>l value := (1 2 3) top[6]=>(set-car! l 4) top[7]=>l value := (4 2 3) top[8]=> top[8]=>(set-cdr! l '(5 6)) top[9]=>l value := (4 5 6) top[10]=> top[10]=>(set-cdr! l l) top[11]=>l value := (4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 ... top[12]=>(set! l '(4 5 6)) value := (4 5 6) top[13]=>l value := (4 5 6) top[14]=>(set-car! l l) top[15]=>l value := (((((((((((((((((((((((((((((((((((((((((((((((((((((... top[16]=> top[16]=> top[16]=>;; local variables are introduced using the LET construct top[16]=> top[16]=>(define (f x) (let ((y 3) (z 7)) (+ x y z))) top[17]=>(f 5) value := 15 top[18]=>(+ 4 (let ((a 3)) a)) value := 7 top[19]=> (define (f x) (lambda (y) (+ x y))) value := f top[20]=>(define g (f 3)) value := g top[21]=>(g 6) value := 9 top[24]=> top[24]=>(define h (let ((count 0)) (lambda () (set! count (+ count 1)) count))) value := h top[25]=>(h) value := 1 top[26]=>(h) value := 2 top[27]=>(h) value := 3 top[28]=>count runtime error: top-level-var `count' not bound break[0]=>(define f h) value := f break[1]=>(f) value := 4 break[2]=>(h) value := 5