Hi John,
Not broken but a WAD (works as designed). You would get the
same sort of error message if you tried to run something
like this: ("a" "b")
It makes sense when you think about what it is that you are
telling Scheme to do. The braces denote the existence of a
procedure within. So your example (()()()) is saying apply
procedure ??? to three empty lists - there is no procedure
to apply and the code falls over. A procedure (list,
append, + etc) lives inside the braces.
If you tried to run this: + then it would resolve to: ;==>
#<primitive:+> which strictly speaking is not an error. In
fact this may be exactly what you want to return.
( (if (< 1 2 ) - +) 3 4) ; ==> -1
while ( (if (> 1 2 ) - +) 3 4) ; ==> 7
Can you work out why that might be?
The only exception (using braces) that won't fall over, is a
single set of empty braces: () which resolves to an empty
list. But, I generally explicitly use '() (a habit) to
remind myself that I really do want to use an empty list -
as in a recursive process on a list. empty and null also
resolve to the same thing too - and empty list.
If you type a string literal like "foo" and then ran it,
you'd see "foo" returned. The string literal is not a
procedure - and aside from being a string, does nothing. So
it resolves to itself: ==> "foo".
Now place the same string literal inside parentheses and see
what happens:
("foo") ; ==> procedure application: expected procedure,
given: "foo" (no arguments)
(you may a slightly different error depending on what you
use)
This is a mistake that I made too when I first starting
using Scheme. A procedure may resolve to a list containing
a sting literal "foo". You will see this result in the
results pane of whatever Scheme flavour you use (I'm using
DrScheme).
So (list "foo") should be read as: apply the procedure list
to the argument "foo". Another shorthand way of saying much
the same thing is to use '("foo" "bar"). I suggest you look
in detail at the use of quote (apostrophe), if you intend
using it for more than something simple like my example.
'("foo" "bar") resolves to ("foo" "bar"). You read this as
a result which is a list containing two string literal
elements. And as I showed you earlier, you can't type the
result and run it ...
(car '("foo" "bar")) ;==> "foo" (a string literal and not a
list).
(cdr '("foo" "bar")) resolves to a list containing the
string literal "bar" or ("bar").
This is important because unless you understand what each
does (list, car , cdr, first, rest) then you can get into
some interesting problems with your code. For example,
depending on your code, you may 'want' to use a list but get
something unexpected.
(define foo (list '("foo" "bar") "rules"))
(car foo);==> ("foo" "bar") - a list containing two string
literals
which is the first element of the list (("foo "bar")
"rules")
(cdr foo); ==> ("rules") - a list containing one string
literal
(car (car foo)) ;==> "foo" - a string literal or the first
element of the first element of the list (("foo "bar")
"rules")
This sort of thing can happen is you are using recursion.
So if you were expecting something else to be returned, your
code falls over. A common learning issue for me when I
first starting using Scheme. It's doing precisely what it
should - but maybe not what you wanted?
I hope that helped. Since I'm learning too - no doubt
others will correct me if I am incorrect on any particular
point.
Kindest regards,
Mike
"Jon Mark" <djangou
@gmail.com> wrote in message
news:1178672420.834877.158250@e51g2000hsg.googlegroups.com...
|I am using Pocket Scheme on a Windows Mobile phone.
|
| Page 5 of the Little Schemer states that (() () () ()) is
a list. But
| entering this expression results in ERROR: not a
procedure.
|
| What am I doing wrong?
|