## Using AND with the apply function in Scheme

and isn’t a normal function because it will only evaluate as few arguments as it needs, to know whether the result is true or false. For example, if the first argument is false, then no matter what the other arguments are, the result has to be false so it won’t evaluate the other arguments. If … Read more

## How does the named let in the form of a loop work?

The basic idea behind a named let is that it allows you to create an internal function, that can call itself, and invoke it automatically. So your code is equivalent to: (define (number->list n) (define (loop n acc) (if (< n 10) (cons n acc) (loop (quotient n 10) (cons (remainder n 10) acc)))) (loop … Read more

## Flatten a list using only the forms in “The Little Schemer”

I have a version that uses only “first-principles” operations and is efficient (does not require more than one pass through any of the lists, unlike append-based solutions). ðŸ™‚ It does this by defining two simple building blocks (fold and reverse), and then defining flatten (and its helper, reverse-flatten-into) atop those (and notice how each function … Read more

## Y combinator discussion in “The Little Schemer”

Great question. For the benefit of those without a functioning DrRacket installation (myself included) I’ll try to answer it. First, let’s use some sane (short) variable names, easily trackable by a human eye/mind: ((lambda (h) ; A. (h h)) ; apply h to h (lambda (g) (lambda (lst) (if (null? lst) 0 (add1 ((g g) … Read more

## How to do a powerset in DrRacket?

What’s in a powerset? A set’s subsets! An empty set is any set’s subset, so powerset of empty set’s not empty. Its (only) element it is an empty set: (define (powerset aL) (cond [(empty? aL) (list empty)] [else As for non-empty sets, there is a choice, for each set’s element, whether to be or not … Read more

## Confused by the difference between let and let* in Scheme

Let is parallel, (kind of; see below) let* is sequential. Let translates as ((lambda(a b c) … body …) a-value b-value c-value) but let* as ((lambda(a) ((lambda(b) ((lambda(c) … body …) c-value)) b-value)) a-value) and is thus creating nested scope blocks where b-value expression can refer to a, and c-value expression can refer to both … Read more

## Count occurrence of element in a list in Scheme?

Your question wasn’t very specific about what’s being counted. I will presume you want to create some sort of frequency table of the elements. There are several ways to go about this. (If you’re using Racket, scroll down to the bottom for my preferred solution.) Portable, pure-functional, but verbose and slow This approach uses an … Read more

## Dot notation in scheme

Note: this is cannibalized from an answer to Recursive range in Lisp adds a period?, which is really asking a different question. However, the explanation of how pairs are printed is the same. The rest of the answer is different. Your question exhibits a bit of misunderstanding, but I think we can clear it up. … Read more

## Which lang packet is proper for SICP in Dr.Racket?

In DrRacket there is a SICP compatibility language 1. From the Package Manager In the documentation there is an easy guide to how it’s installed from DrRacket: Open the Package Manager: in DrRacket choose the menu “File” then choose “Package Manager…”. In the tab “Do What I Mean” find the text field and enter: “sicp” … Read more

## Unexpected persistence of data [duplicate]

Are you doing something like this: CL-USER> (defun foo () (let ((value ‘(1))) ; ‘(1) is literal data (incf (car value)))) FOO CL-USER> (foo) 2 CL-USER> (foo) 3 CL-USER> (foo) 4 CL-USER> (foo) 5 Quoted data is literal data; there’s only one copy of it, and the consequences of modifying it are undefined. The behavior … Read more