Completed 2.23

This commit is contained in:
Oliver Payne 2021-08-30 21:19:17 +01:00
parent a6b0a5b3b5
commit 751556bc37
3 changed files with 22 additions and 0 deletions

10
2_21.sch Normal file
View File

@ -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))

8
2_22.txt Normal file
View File

@ -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.

4
2_23.sch Normal file
View File

@ -0,0 +1,4 @@
(define (for-each f l)
(if (not (null? l))
(begin (f (car l))
(for-each f (cdr l)))))