# The Little Schemer, Chapter 8

**URL:** <https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120>\
**Category:** Friendly\
**Tags:** learning, coding\
**Created:** [May 18, 2021, 11:10pm UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120 "2021-05-18T23:10:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AnneB](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/anneb/32/15_2.png) [@AnneB](https://discuss.criticalfallibilism.com/u/AnneB)\
**Post date:** [May 18, 2021, 11:10pm UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/1 "2021-05-18T23:10:53Z")

</div>

I’ve been working through _The Little Schemer_, by Daniel P. Friedman and Matthias Felleisen. I’m up to Chapter 8 now.

Justin has been through the book and posted about it on his old blog. His post for Chapter 8 is [here](https://oldblog.justinmallone.com/2021/04/the-little-schemer-chapter-8/). He made a video about his work on Chapter 8, which is [here](https://www.youtube.com/watch?v=3pQCBGOChGc&t=684s).

I haven’t posted my work on The Little Schemer anywhere. But I’ll post some comments and questions about Chapter 8 in this thread.

---

<div class="post-metadata">

**Author:** ![AnneB](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/anneb/32/15_2.png) [@AnneB](https://discuss.criticalfallibilism.com/u/AnneB)\
**Post date:** [May 18, 2021, 11:14pm UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/2 "2021-05-18T23:14:09Z")

</div>

Two things I noticed so far when I watched Justin’s video:

1. Justin does more testing than I do. Maybe I should do more.
2. When Justin is confused about something, he stays calm, whereas I sometimes start to feel frustrated. He has confidence that he can make progress in figuring the thing out. I sometimes don’t think I can make any progress–I either get it after reading it through a few times or I will never get it. I don’t want to think that way.

---

<div class="post-metadata">

**Author:** ![AnneB](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/anneb/32/15_2.png) [@AnneB](https://discuss.criticalfallibilism.com/u/AnneB)\
**Post date:** [May 18, 2021, 11:20pm UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/3 "2021-05-18T23:20:49Z")

</div>

Question:

In [Justin’s video](https://www.youtube.com/watch?v=3pQCBGOChGc&t=684s), starting around 11:00, the question in the book (p. 127) is “Can you say what `(lambda (a l) …)` is?” Justin says “`lambda` returns a function that takes the arguments `a` and `l`”. I would have said “`(lambda (a l) …)` is a function”. Are both true? I don’t get how `lambda` returns a function. Is that the same as saying that having `lambda` at the beginning of `(lambda ….)` indicates that the whole thing is a function?

[code formatting added by me to the quotations]

---

<div class="post-metadata">

**Author:** ![JustinCEO](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/justinceo/32/9_2.png) [@JustinCEO](https://discuss.criticalfallibilism.com/u/JustinCEO)\
**Post date:** [May 19, 2021, 12:03am UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/4 "2021-05-19T00:03:13Z")

</div>

> [@AnneB](#):
>
> When Justin is confused about something, he stays calm, whereas I sometimes start to feel frustrated. He has confidence that he can make progress in figuring the thing out. I sometimes don’t think I can make any progress–I either get it after reading it through a few times or I will never get it. I don’t want to think that way.

The mindfulness meditation stuff can help with getting upset overall.

There’s also some generic stuff that applies to coding too. Like I think some generic issues people have when learning things are:

1. Not having a reasonable expectation regarding the amount of work that can be expected (and feeling dumb when they have to do a lot of work).
2. Not knowing how to break a problem down into small components.
3. Underestimating the extent to which a large problem can be approached in small chunks, by e.g. working on small portions of it, writing analyses of what you do know, working on related skills, and so on.

But anyways this is all stuff that’s solvable.

FWIW i still get upset sometimes. Though not for very long, at least about this stuff. I mentioned an example here that was very recent:

> [@Justin Does SICP](https://discuss.criticalfallibilism.com/t/justin-does-sicp/96/10):
>
> That actually helps make clear part of the reason I like the blogging format rather than trying to have a bunch of answers in a more organized/polished presentation - cuz I treat the blogging format more like a journal of my thoughts as they are happening and find it easy to talk about misconceptions or errors that come out in that context. It’s not supposed to be like a final polished thing. When I realized I made an error today, initially I got kind of upset about it, but then I thought abou…

---

<div class="post-metadata">

**Author:** ![JustinCEO](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/justinceo/32/9_2.png) [@JustinCEO](https://discuss.criticalfallibilism.com/u/JustinCEO)\
**Post date:** [May 19, 2021, 12:16am UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/5 "2021-05-19T00:16:18Z")

</div>

> [@AnneB](#):
>
> Question:
> 
> In [Justin’s video](https://www.youtube.com/watch?v=3pQCBGOChGc&t=684s), starting around 11:00, the question in the book (p. 127) is “Can you say what `(lambda (a l) …)` is?” Justin says “`lambda` returns a function that takes the arguments `a` and `l`”. I would have said “`(lambda (a l) …)` is a function”. Are both true? I don’t get how `lambda` returns a function. Is that the same as saying that having `lambda` at the beginning of `(lambda ….)` indicates that the whole thing is a function?
> 
> [code formatting added by me to the quotations]

A real programmer should feel free to jump in and tell me I’m wrong cuz I’m a newb and basing this off like one book.

I _think_ that we say that a lambda returns a procedure, and that procedure is considered an anonymous procedure or unnamed procedure.

Here’s what happens when I run

`(lambda (x) (* x x))`

in DrRacket:

![image](https://discuss.criticalfallibilism.com/uploads/default/original/1X/2ec9d7219084b50fd4927d8f1647e3c29294f12d.png)

This is typical output if you just call a procedure without parentheses, btw. You’re evaluating the value of the procedure name but not invoking it. See e.g. `list`

![image](https://discuss.criticalfallibilism.com/uploads/default/original/1X/0db32c8d227452d01344deef9e993a520a226ed8.png)

But now consider:

`((lambda (x) (* x x)) 2)`

![image](https://discuss.criticalfallibilism.com/uploads/default/original/1X/06310e44dcdb123ca1d93e05c16bd3a99e73976e.png)

Now we’re taking that procedure returned by the lambda and actually giving it an argument. So this is kinda more like a procedure invocation

Simply Scheme has a whole chapter on lambda you might wanna revisit:

[https://people.eecs.berkeley.edu/~bh/ssch9/lambda.html](https://people.eecs.berkeley.edu/~bh/ssch9/lambda.html)

---

<div class="post-metadata">

**Author:** ![Max](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/max/32/97_2.png) [@Max](https://discuss.criticalfallibilism.com/u/Max)\
**Post date:** [May 19, 2021, 3:17am UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/6 "2021-05-19T03:17:27Z")

</div>

> [@JustinCEO](#):
>
> A real programmer should feel free to jump in and tell me I’m wrong cuz I’m a newb and basing this off like one book.
> 
> I _think_ that we say that a lambda returns a procedure, and that procedure is considered an anonymous procedure or unnamed procedure.

I wouldn’t say that lambda _returns_ a procedure, but I haven’t done much lisp. My intuition is to say that it _defines an anonymous function_. That said, this sort of thing can be language specific. (note: I think _procedure_ = _function_ in this case, but IDK maybe lisp/scheme uses the terms to mean diff stuff)

> `Lambda` is the name of a special form that generates procedures. It takes some information about the function you want to create as arguments and it returns the procedure.  
> …  
> The first argument to `every` is, in effect, the same procedure as the one we called `add-three` earlier, but now we can use it without giving it a name. (Don’t make the mistake of thinking that `lambda` is the argument to `every` . The argument is _the procedure returned by_ `lambda`.)

From: [https://people.eecs.berkeley.edu/~bh/ssch9/lambda.html](https://people.eecs.berkeley.edu/~bh/ssch9/lambda.html)

So it seems fine to say that the function is _returned_ by `lambda`. It sounds odd to me, tho, b/c I wouldn’t use that term for anon functions in other languages. _Generated_ sounds better. That said, I think most programmers wouldn’t have trouble following along if you were explaining something and talked about `lambda` returning a function (there’s excess capacity in the term).

---

<div class="post-metadata">

**Author:** ![Elliot](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/elliot/32/14_2.png) [@Elliot](https://discuss.criticalfallibilism.com/u/Elliot)\
**Post date:** [May 19, 2021, 3:21am UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/7 "2021-05-19T03:21:54Z")

</div>

> [@Max](#):
>
> I wouldn’t say that lambda _returns_ a procedure, but I haven’t done much lisp.

Lambda both creates and returns a procedure (aka function, same thing). The created procedure is the return value of the lambda call. (Source: I took SICP from Brian Harvey, author of Simply Scheme, and was his TA twice.)

---

<div class="post-metadata">

**Author:** ![AnneB](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/anneb/32/15_2.png) [@AnneB](https://discuss.criticalfallibilism.com/u/AnneB)\
**Post date:** [May 19, 2021, 6:49am UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/8 "2021-05-19T06:49:02Z")

</div>

Okay. So `lambda` both creates and returns a procedure.

Can I say that `(lambda (x) (* x x))` (the whole thing, not just the word lambda) _is_ a procedure? Or is that bad wording?

---

<div class="post-metadata">

**Author:** ![Max](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/max/32/97_2.png) [@Max](https://discuss.criticalfallibilism.com/u/Max)\
**Post date:** [May 19, 2021, 7:09am UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/9 "2021-05-19T07:09:59Z")

</div>

> [@AnneB](#):
>
> Can I say that `(lambda (x) (* x x))` (the whole thing, not just the word lambda) _is_ a procedure? Or is that bad wording?

Yes, tho IDK if that’s _technically_ correct. Either way I don’t think it’s a bottleneck.

Since the procedure is anonymous, it’s hard to talk about it otherwise. If we were sitting at a computer together looking at a page of source code, and I asked “which procedure?”, then a reasonable answer would be for you to select the whole thing and say “this one”.

---

<div class="post-metadata">

**Author:** ![JustinCEO](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/justinceo/32/9_2.png) [@JustinCEO](https://discuss.criticalfallibilism.com/u/JustinCEO)\
**Post date:** [May 19, 2021, 12:10pm UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/10 "2021-05-19T12:10:25Z")

</div>

> [@AnneB](#):
>
> Okay. So `lambda` both creates and returns a procedure.
> 
> Can I say that `(lambda (x) (* x x))` (the whole thing, not just the word lambda) _is_ a procedure? Or is that bad wording?

I think that’s okay. I’ve also loosely referred to like, “the lambda” when talking about what the procedure returned by a lambda does (e.g. “the lambda here squares the argument it’s given”, something like that). So I kinda use lambda like a name sometimes, informally. I think that technically the lambda creates/returns an unnamed procedure that does whatever operation like squaring an argument, but it’s hard to be super technically precise all the time (though might be worth trying to be that precise some, especially if things are fuzzy)

---

<div class="post-metadata">

**Author:** ![AnneB](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/anneb/32/15_2.png) [@AnneB](https://discuss.criticalfallibilism.com/u/AnneB)\
**Post date:** [May 19, 2021, 7:25pm UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/11 "2021-05-19T19:25:10Z")

</div>

New question:

Did the authors make an error in their `insertL-f` procedure (and similarly in `insertR-f`)?

`insertL-f` inserts `new` to the left of the first thing in `l` that passes the `test?` with `old`:

```auto
(define insertL-f
  (lambda (test?)
    (lambda (new old l)
      (cond ((null? l) (quote ()))
            ((test? (car l) old) (cons new (cons old (cdr l))))
            (else (cons (car l) ((insertL-f test?) new old (cdr l))))))))

```

Their examples have `test?` being `eq?` or some other version of equality. In that case, their procedure works.

But if `test?` is something like “is the element of length `old`?”, then it doesn’t work. On the next-to-last line, instead of `old`, they should have `(car l)`, right?

Or am I misunderstanding?

---

<div class="post-metadata">

**Author:** ![Max](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/max/32/97_2.png) [@Max](https://discuss.criticalfallibilism.com/u/Max)\
**Post date:** [May 19, 2021, 11:46pm UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/12 "2021-05-19T23:46:50Z")

</div>

> [@JustinCEO](#):
>
> I’ve also loosely referred to like, “the lambda” when talking about what the procedure returned by a lambda does (e.g. “the lambda here squares the argument it’s given”, something like that). So I kinda use lambda like a name sometimes, informally.

This is fine.

The term _lambda_ comes from [lambda calculus](https://en.wikipedia.org/wiki/Lambda_calculus). Lambda calculus is an area of maths about particular ways of writing (like \lambda x.\*xx) and reducing functions – and those methods are foundational to lisps and other functional languages (like Haskell). From the linked wiki page:

> A valid lambda calculus expression is called a “lambda term”.

So basically I think calling the lambda expression (i.e., the full function) a _lambda_ is fine. If you wanted to be more explicit you could say “lambda expression” or “lambda function”.

I wanted to mention all that b/c the general concept of (and words used for) a lambda expression overlaps with scheme’s `lambda` special form. In other languages the word `lambda` isn’t a reserved keyword, but you can still write lambda functions, and you could still call them lambdas. e.g., these two expressions do the same thing (the first in scheme, the second in haskell, and third as a lambda expression): `(lambda (x) (* x x))` and `\x -> x * x` and \lambda x.\*xx.

> [@JustinCEO](#):
>
> but it’s hard to be super technically precise all the time

Yeah, there are lots of things that programmers skip over or shortcut in this way – it’s not a big deal mostly. An example is how ppl talk about a function’s arguments and parameters – usually ppl treat those terms as interchangeable. I think _technically_ “arguments” are the values that get passed in to a function call, and “parameters” are the input variables that a function takes. e.g.: (in python)

```python
# declare a function `f` with a single parameter: `x`
def f(x):
  return x*x

def main():
  # call `f` with the argument `3`
  return f(3)

```

---

<div class="post-metadata">

**Author:** ![AnneB](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/anneb/32/15_2.png) [@AnneB](https://discuss.criticalfallibilism.com/u/AnneB)\
**Post date:** [May 20, 2021, 1:09pm UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/13 "2021-05-20T13:09:15Z")

</div>

> [@Max](#):
>
> Yeah, there are lots of things that programmers skip over or shortcut in this way – it’s not a big deal mostly. An example is how ppl talk about a function’s arguments and parameters – usually ppl treat those terms as interchangeable. I think _technically_ “arguments” are the values that get passed in to a function call, and “parameters” are the input variables that a function takes.

I prefer to get this kind of thing right, especially while I’m a beginner, because it helps me understand better. I’ve been using “argument” wrong and I’ll try to fix that going forward.

---

<div class="post-metadata">

**Author:** ![JustinCEO](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/justinceo/32/9_2.png) [@JustinCEO](https://discuss.criticalfallibilism.com/u/JustinCEO)\
**Post date:** [May 20, 2021, 4:15pm UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/14 "2021-05-20T16:15:11Z")

</div>

> [@AnneB](#):
>
> On the next-to-last line

off-topic but i wonder how easy it would be to turn on good syntax highlighting + line numbers for discourse. it was pretty easy to do for my blog

---

<div class="post-metadata">

**Author:** ![JustinCEO](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/justinceo/32/9_2.png) [@JustinCEO](https://discuss.criticalfallibilism.com/u/JustinCEO)\
**Post date:** [May 20, 2021, 4:20pm UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/15 "2021-05-20T16:20:52Z")

</div>

> [@AnneB](#):
>
> But if `test?` is something like “is the element of length `old` ?”, then it doesn’t work. On the next-to-last line, instead of `old` , they should have `(car l)` , right?

I’m not quite following. You think instead of…

`(cons new (cons old (cdr l))))`

…it should be…

`(cons new (cons (car l) (cdr l))))`

…do I have that right? I’m not sure I see why. Can you try putting in code the case for which you think their approach wouldn’t work?

---

<div class="post-metadata">

**Author:** ![AnneB](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/anneb/32/15_2.png) [@AnneB](https://discuss.criticalfallibilism.com/u/AnneB)\
**Post date:** [May 20, 2021, 4:52pm UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/16 "2021-05-20T16:52:17Z")

</div>

I should have thought to give an example. Here’s one:

```auto
(define divisible-by?
  (lambda (x y)
    (integer? (/ x y))))

> ((insertL-f divisible-by?) 0 2 '(3 4 5 6))
'(3 0 2 5 6)

```

The program is meant to insert a 0 before the first thing in the list that’s divisible by 2. It did that, but it also changed the thing that’s divisible by 2 to a 2 because `old` is 2. `car l` at that point would be 4. Does this make sense?

---

<div class="post-metadata">

**Author:** ![AnneB](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/anneb/32/15_2.png) [@AnneB](https://discuss.criticalfallibilism.com/u/AnneB)\
**Post date:** [May 20, 2021, 5:02pm UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/17 "2021-05-20T17:02:49Z")

</div>

> [@AnneB](#):
>
> I should have thought to give an example.

More importantly, I should have thought to try out some examples on my own. I was just going by reading the code. I should be trying things out more.

---

<div class="post-metadata">

**Author:** ![JustinCEO](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/justinceo/32/9_2.png) [@JustinCEO](https://discuss.criticalfallibilism.com/u/JustinCEO)\
**Post date:** [May 20, 2021, 5:04pm UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/18 "2021-05-20T17:04:22Z")

</div>

i think the `insert` procedures had in mind the context that you were trying to insert a _new_ value on the basis of whether a particular element in the list that was pre-existing - the _old_ value - passed a test.

i am looking at your example. it seems like, if we follow the substitution model, the formal parameters within the lambdas of `insertL-f` would be misnamed. e.g. **2** would be substituted in for _old_. But 2 is not actually a value that appears in the list _l_.

---

<div class="post-metadata">

**Author:** ![JustinCEO](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/justinceo/32/9_2.png) [@JustinCEO](https://discuss.criticalfallibilism.com/u/JustinCEO)\
**Post date:** [May 20, 2021, 5:05pm UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/19 "2021-05-20T17:05:53Z")

</div>

if the parameters appear misnamed for a particular use-case, that seems like a sign that maybe we are trying to get the procedure to do stuff that was not within the anticipated scope of the procedure as it was designed

---

<div class="post-metadata">

**Author:** ![AnneB](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/anneb/32/15_2.png) [@AnneB](https://discuss.criticalfallibilism.com/u/AnneB)\
**Post date:** [May 20, 2021, 5:34pm UTC](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120/20 "2021-05-20T17:34:28Z")

</div>

Good point that the parameters seem to be misnamed for how I’m thinking about it, so maybe they’re not intending what I have in mind.

The only way I can make sense of their procedure is if they meant `test?` to always be a test for equality. Depending on what kind of things you wanted to know the equality of, you’d use a different procedure for testing that equality.

This seems limiting to me, but it makes their procedure okay, and they never talked about any other kind of test. So I think you’re right that that’s what they intended.

[Next page](https://discuss.criticalfallibilism.com/t/the-little-schemer-chapter-8/120.md?page=2)
