#lang racket #| Max Sherman CSE 341 Section 1 Things to do: 1. Introductions (Data structure, programming language, function or method, color, number) 2. Example functions 3. Practice writing racket 4. Demo Notes about map: 2 arguments and returns a new list: map f lst -> (new list) Example call: (map (lambda (x) (+ x 1)) '(1 2 3 4)) ----> '(2 3 4 5) Alternatively we can use the function add1 to do the same thing: (map add1 '(1 2 3 4)) ----> '(2 3 4 5) |# (define (map f lst) (if (null? lst) null (cons (f (car lst)) (map f (cdr lst))))) ;; "Tail recursive map". This reverses the list so it isn't really map. (define (map-tr f lst) (letrec ([map* (lambda (f lst acc) (if (null? lst) acc (map* f (cdr lst) (cons (f (car lst)) acc))))]) (map* f lst null))) (define (reverse lst) (letrec ([reverse* (lambda (lst acc) (if (null? lst) acc (reverse* (cdr lst) (cons (car lst) acc))))]) (reverse* lst null))) (define (sum lst) (letrec ([sum* (lambda (lst acc) (if (null? lst) acc (sum* (cdr lst) (+ (car lst) acc))))]) (sum* lst 0)))