;; CSE 413 23sp ;; Lecture 4 more list functions, scope, function definitions and examples #lang racket (define magic 42) (define xvii 17) (define pi 3.1415926535) ;; some sample lists (define nums '(5 17 3 12)) (define ab '(a b)) (define xyz '(x y z)) (define stuff '(1 banana (2 3) 4 (5))) ;; Functions on lists ;; done previously: list functions: cons, car, cdr; list, quoting ;; also done previously: predicates: null?, pair?, list? and functions like length, sum ;; from last time - append ;; First attempt to append lists - buggy (define (badapp x y) (cons x y)) ;; Fixed append (define (app x y) (if (null? x) y (cons (car x) (app (cdr x) y)))) ;; new - diagram/trace (app ab xyz) being careful about scopes (global, local fcn) ;; new - attempt to reverse a list (define (rev1 x) (if (null? x) '() (cons (rev1 (cdr x)) (car x)))) (define (rev2 x) (if (null? x) '() (append (rev2 (cdr x)) (car x)))) (define (rev3 x) (if (null? x) '() (append (rev3 (cdr x)) (list (car x)))))