diff --git a/2_21.sch b/2_21.sch new file mode 100644 index 0000000..d59da13 --- /dev/null +++ b/2_21.sch @@ -0,0 +1,10 @@ +(define (square x) (* x x)) + +(define (square-list-1 items) + (if (null? items) + '() + (cons (square (car items)) (square-list-1 (cdr items))))) + +(define (square-list-2 items) + (map square items)) + diff --git a/2_22.txt b/2_22.txt new file mode 100644 index 0000000..4f1f977 --- /dev/null +++ b/2_22.txt @@ -0,0 +1,8 @@ +The list is in the reverse order because each call to cons puts the next +item in front of the previous one. We are iterating through the input +list from left to right but are adding to the result from right to left. + +The second solution doesn't work because cons takes a single value as +its first value and a list as its second. + + diff --git a/2_23.sch b/2_23.sch new file mode 100644 index 0000000..2736bb6 --- /dev/null +++ b/2_23.sch @@ -0,0 +1,4 @@ +(define (for-each f l) + (if (not (null? l)) + (begin (f (car l)) + (for-each f (cdr l)))))