[Programming] [Scheme] Confused about something in The Little Schemer Chapter 9

14 views
Skip to first unread message

Justin Mallone

unread,
Apr 16, 2021, 10:28:10 AM4/16/21
to FIGG
I'm confused about something in The Little Schemer Chapter 9.

I was gonna write about the substance here, but then I realized I wanted
to screenshot images from the book. I decided that posting on my blog
and linking the post would be the best solution, so:
https://justinmallone.com/2021/04/the-little-schemer-discussion-of-part-of-chapter-9/

I tried to go through small parts and say what I did and didn't
understand about each.

-JM

Elliot Temple

unread,
Apr 16, 2021, 2:51:30 PM4/16/21
to FIGG
It’s hard to tell what you wrote and what’s from the book, and also it’d be helpful to state what “shift” and “build” do in words. This “shift” appears to be substantially different than the ruby function by the same name.

Elliot Temple
www.fallibleideas.com

Justin Mallone

unread,
Apr 16, 2021, 3:08:16 PM4/16/21
to FIGG

On Apr 16, 2021, at 14:51 PM, Elliot Temple <cu...@curi.us> wrote:

> On Apr 16, 2021, at 7:28 AM, Justin Mallone <jus...@justinmallone.com>
> wrote:
>
>> I'm confused about something in The Little Schemer Chapter 9.
>>
>> I was gonna write about the substance here, but then I realized I
>> wanted to screenshot images from the book. I decided that posting on
>> my blog and linking the post would be the best solution, so:
>> https://justinmallone.com/2021/04/the-little-schemer-discussion-of-part-of-chapter-9/
>>
>> I tried to go through small parts and say what I did and didn't
>> understand about each.
>
> It’s hard to tell what you wrote and what’s from the book,

Oh that's useful feedback. I put a black border around my images at some
point to make them stick out on the page more - maybe I should make it
thicker and be more careful about spacing my own text so that it stands
apart more.

> and also it’d be helpful to state what “shift” and “build”
> do in words. This “shift” appears to be substantially different
> than the ruby function by the same name.

Their description of shift is "The function shift takes a pair whose
first component is a pair and builds a pair by shifting the second part
of the first component into the second component."

So for (shift '((a b) c)) you get (a (b c)) as output,
or for (shift ((a b) (c d)) you get (a (b (c d))) as output.

The code for shift is:

(define shift
(lambda (pair)
(build (first (first pair))
(build (second (first pair))
(second pair)))))

I think build is more straightforward - I can use my own words. Build
takes two arguments, and cons'es the first argument onto the result of
cons'ing the second argument onto the empty list:

(define build
(lambda (s1 s2)
(cons s1 (cons s2 '()))))

-JM
Reply all
Reply to author
Forward
0 new messages