views:

821

answers:

4

I'm trying to learn Scheme from the book "The Little Schemer" using DrScheme on a Macintosh. It starts with things like "What is the car of l where l is the argument (a b c)?"

I understand that the answer to this question is a, but I'm not able to actually figure out what to type into Dr Scheme to "follow along". A simple idea on how to assign to l such as > (def l ('a 'b 'c))

gives me the error: "function call: expected a defined name or a primitive operation name after an open parenthesis, but found something else"

+1  A: 

Try this:

(define l '(a b c))

Here, the quote applies to the whole list (a b c).

Greg Hewgill
Error message: def: name is not defined, not a parameter, and not a primitive name
Leonard
It's supposed to be "define" not "def", see my answer for more info: http://stackoverflow.com/questions/464057/assigning-a-list-of-atoms-in-scheme#464072
Kyle Cronin
er, I was just following your example. Changed to 'define'.
Greg Hewgill
+1  A: 

Or, just use

(car '(a b c))

What the error is telling you is this: when the reader sees a list, it wants to treat the first element of the list as a function. That's how (+ 1 2 3) works: it invokes hthe function +. (Strictly, it looks at the symbol '+ and finds that there is a function bound to that, then invokes that function.)

So, when you type

(define l ('a 'b 'c))

it looks at the inner list and then wants to find a function. Instead it find s the symbol named a. Since there isn't a function there, you get the error. If, instead, you type

(define l '(a b c))

you've told the reader via the quote that it's to treat that as a list without trying to interpret it.

Here's an example from MIT Scheme on a Mac:

1 ]=> (define li '(a b c))

;Value: li

1 ]=> (car li)

;Value: a

1 ]=> (car '(a b c ))

;Value: a
Charlie Martin
> then wants to find a function named a [...]Strictly speaking, that's not true. Even if you had a function named a it would still be an error because it's not looking for the value a, but attempting to use the symbol a. It's always an error to try to use a symbol as an operator.
Kyle Cronin
True enough. it was late.
Charlie Martin
+3  A: 

To define something:

(define <name> <value>)

So to define l:

(define l '(a b c))

This defines l as the list (a b c). The single quote mark quotes whatever is after it, whether it's a symbol or a list, which means it's not evaluated but read as-is. You don't, however, quote the name that you're setting it to. Thankfully, this is one of the very small number of operations in Scheme where you have this inconsistency.

Kyle Cronin
This works after I change the language selected from "Beginning Student" to "Beginning Student with list abbreviations".Thanks.
Leonard
I use DrScheme quite a bit, but the student limitations are just stupid. Instead of telling you you're using a more advanced feature, it just throws an error. My suggestion would be to avoid the student stuff and just use the unabridged Scheme. Use Module and preface all your files with #lang scheme
Kyle Cronin
Um, sounds good but I can't get this to work. I get the error message:standard-module-name-resolver: collection not found: "scheme" in any of: (#<path:/Users/lcuff/Library/PLT Scheme/372/collects> #<path:/Applications/PLT Scheme v372/collects>)
Leonard
Sorry, I thought you were using a newer copy of DrScheme! With version 4 they standardized on the Module language; for earlier versions I would suggest using Pretty Big (or upgrading).
Kyle Cronin
The largest compatibility issue with version 4+ is the removal of set-car! and set-cdr!. They were "replaced" with set-mcar! and set-mcdr!, and can only operate on a "mutable" pair, created with mcons (as opposed to cons). As long as you're mindful of this change you should be all set.
Kyle Cronin
> I use DrScheme quite a bit, but the student limitations > are just stupid.The student limitations are a god sent gift, when teaching from HtDP.If you are not following HtDP, then don't use the teaching languages - use the module language instead and start your file with #scheme
soegaard
A: 

Non-abbraveted form of

(define l '(a b c))

is

(define l (quote (a b c)))

or in this case even this:

(define l (list 'a 'b 'c))

without a "list" part it tries to execute 'a, obviously

Slartibartfast