Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Scheme Programming Language

List of Empty List Example From Little Schemer Doesn't Work On Pocket Scheme


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?

Jon Mark wrote:
> 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?

What the book is showing you is the printed representation of a list of
empty lists.

 If you want to evaluate an expression that has this as its result, you
need to quote the list, otherwise scheme will try to evaluate it as a
normal form. Try typing in this (note that the first character is a
single quote):

'(() () () ())

The quote character basically tells scheme to "create a list of the
following literal items". This is actually shorthand for the more
verbose:

(quote (() () () ()))

Which should give the same results. Or you could use:

(list '() '() '() '())

Which says "create a list consisting of the values of the following
items. In this case, you provide expressions after the identifier
"list" (notice the quote again) that resolves to an empty list.

Isn't scheme fun?

Brian.

--

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?
|

Mike writes:
> 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.

Your habit is good. Of those four, only '() is officially in
R5RS. An unquoted pair of parens is not an _expression_ at
all, it's just a written representation.

Some, maybe many, implementations define () the way you say.
I don't know about the forthcoming R6RS - I think I saw what
was decided but then I forgot.

Jon Mark <djangou@gmail.com> writes:
> 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?

You're believe a list is a form.  Not all lists are forms, and not all
forms are lists.

list = data
form = program

For a list to be a form it needs to be a valid program, that is, it's
first item must be a syntax symbol, or must evaluate to a
procedure.  () is neither.

A form is either an atom, or a list whose first item is a syntax
symbol, or evaluates to a procedure.

C/SCHEME[3]> (let ((object (read)))
               (newline) (display "Object read:  ") (display object)
               (newline) (display "Is it a list? ") (display (list? object)))
(() () () ())

Object read:  (() () () ())
Is it a list? #T

You can build such a list in a program for example with the list procedure:

(list (quote ()) (quote ()) (quote ()) (quote ()))
; this returns a new list with 4 () elements.

Or you can just express it literally:

(quote (() () () ()))
; this returns the list  (() () () ())  that is the argument of quote.

--
__Pascal Bourguignon__                     http://www.informatimago.com/

"This statement is false."            In Lisp: (defun Q () (eq nil (Q)))

On May 8, 6:00 pm, Jon Mark <djangou@gmail.com> wrote:

> Page 5 of the Little Schemer states that (() () () ()) is a list. But
> entering this expression results in ERROR: not a procedure.

I apologize for the lousy error message.  If you had attempted to
evaluate the equally invalid combinations (#f 22) or ('(12 foo) 22),
you'd get the somewhat more enlightening messages "ERROR: not a
procedure: #f" and "ERROR: not a procedure: (12 foo)."

> What am I doing wrong?

You're entering generic data as an expression to be evaluated.  Not
every list datum is executable, despite the book's admonition in the
Preface to "try the examples while you read."  If you type an
expression into Pocket Scheme and tap the green lambda, Pocket Scheme
attempts to evaluate that expression, i.e., run it as code.

Looking back in T.L.S. five entries before the (() () () ()) example
that gave you trouble, did you also attempt to evaluate the list "(how
are you doing do far)"?  Presumably then you also saw an error mesage
"ERROR: unbound variable: how."  Similar issue.

Anyway, once you get out of the book's first is-this-an-atom, is-this-
a-list exercises, most of its examples will be evaluable.  The only
other trap that you need to avoid is, when the book asks (e.g.), "What
is the value of (car l) where l is (its peanut butter jelly time)", if
you want to type this into the Scheme input panel, you cannot simply
substitute (its peanut butter jelly time) for l in the Scheme input
panel like (car (its peanut butter jelly time)).  Instead, you need to
*quote* the substitution -- observe the single-tick mark that I've
added to (car '(its peanut butter jelly time)) -- to tell Scheme to
treat (its peanut butter jelly time) as literal data instead of a
subexpression to evaluate.

(A raw Scheme REPL isn't the very best accompaniment for T.L.S., is
it?  Maybe someday....)

I hope that made some sense.  Good luck with Scheme.

Mike  writes:
> [...] So your example (()()()) is saying apply
> procedure ??? to three empty lists - there is no procedure
> to apply and the code falls over.

Indeed, the code says "apply () to () and ()".  The interpreter's
message "not a procedure" refers to the first (), which indeed does
not indeed evaluate to a procedure.

--
Emlio C. Lopes                            Ich leb und wei nit wie lang,
Munich, Germany                            ich stirb und wei nit wann,
                                           ich fahr und wei nit wohin,
                 (Martinus von Biberach)   mich wundert, dass ich frhlich bin!

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc