From 751556bc37808beced22b3ae9ebd8921141b3ecc Mon Sep 17 00:00:00 2001 From: Oliver Payne Date: Mon, 30 Aug 2021 21:19:17 +0100 Subject: [PATCH] Completed 2.23 --- 2_21.sch | 10 ++++++++++ 2_22.txt | 8 ++++++++ 2_23.sch | 4 ++++ 3 files changed, 22 insertions(+) create mode 100644 2_21.sch create mode 100644 2_22.txt create mode 100644 2_23.sch 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)))))