LISP SICP Video Lecture 2a Average Damp Question

The average-damp procedure takes a procedure as its argument and returns a procedure as its value. When given a procedure that takes one argument, average-damp returns another procedure that computes the average of the values before and after applying the original function f to its argument. It’s inside the fixed-point procedure where that returned function … Read more

Count occurrence of element in a list in Scheme?

In Racket, you could do But more seriously, doing this with lists in Scheme is much easier that what you mention. A list is either empty, or a pair holding the first item and the rest. Follow that definition in code and you’ll get it to “write itself out”. Here’s a hint for a start, … Read more

Scheme IDE for Windows

DrScheme is the best Scheme IDE I have found. It has a JIT compiler, can build stand-alone executables or run in the traditional Scheme REPL, has smart syntax editing , as well as other traditional IDE features like Menu bars to change features. Running/stopping your program is as easy as clicking ‘Run’/’Stop’. It also includes a macro-expander … Read more

Help explaining how `cons` in Scheme work?

cons build pairs, not lists. Lisp interpreters uses a ‘dot’ to visually separate the elements in the pair. So (cons 1 2) will print (1 . 2). car and cdr respectively return the first and second elements of a pair. Lists are built on top of pairs. If the cdr of a pair points to another pair, that sequence is treated as a list. The cdr of … Read more

What is the difference between map and apply in scheme?

They are not the same! Their names can actually help remember which does what. map will take as argument one procedure and one or more lists. The procedure will be called once for each position of the lists, using as arguments the list of elements at that position: map called (- 2), (- 3), (- … Read more

In Scheme, what’s the point of “set!”?

Though both define and set! will redefine a value when in the same scope, they do two different things when the scope is different. Here’s an example: As you can see, when we create a new lexical scope (such as when we define a function), any names defined within that scope mask the names that … Read more

“cond”,”and” and “or” in Scheme

Imagine you wrote if as a function/procedure rather than a user defined macro/syntax: The problem with my-if is that as a procedure every argument gets evaluated before the procedure body gets executed. thus in atom? the parts (not (pair? x)), #t and #f were evaluated before the body of my-if gets executed. For the last example means (add (- a 1) (+ b 1)) gets evaluated regardless of what a … Read more

How does `let` work in Scheme?

While not exactly the problem you’re experiencing, but an aside based on your questioning about the sequence of evaluating the arguments, let is also “syntactic sugar” for a lambda followed by it’s arguments that are first evaluated and then passed to the lambda which is then evaluated. For instance: is the same as: So, if you’re ever … Read more

What is the best Scheme interpreter or compiler?

For a beginner, I would highly recommend DrRacket (formerly Dr. Scheme), since it gives you a really nice environment to work in, supports many dialects of Scheme, and gives very good failure and debugging information. I believe most implementations of Scheme are interpreters, although it is possible that there is a compiler out there. If you are … Read more