[racket] lists vs strings

From: Norman Gray (norman at astro.gla.ac.uk)
Date: Tue Aug 5 19:10:09 EDT 2014

jseb, hello.

On 2014 Jul 27, at 12:51, jseb <gmane2013 at finiderire.com> wrote:

> But i read that quote character is an alias for «list». Are there cases
> where it has different meaning ? (oh no, magic again :/ ).

No, it's not an alias for 'list'; instead, it's syntactic sugar for 'quote' which is a form which, when evaluated, gives you back exactly what you gave it.

> (quote (1 + list))
'(1 + list)
> (quote +)
'+

A possibly better way to think of it is that quote precedes a lisp 'literal' -- the thing after it is not expanded in any way.

These are two slightly informal ways of explaining this.  With Scheme-family languages (eg Racket) it's a _really_ good idea to be _very_ secure in your understanding of how expressions are evaluated.  The 'Racket Essentials' chapter in the Guide <http://docs.racket-lang.org/guide/to-scheme.html> works through that in some detail.

There's actually very little magic in Schemes -- generally much less than there is in other languages, I feel -- but it can quite often look like magic if you're uncertain how the few rigid rules are applied.

> If it's strictly an alias, this one shoud returns the same result:
> 
>> (list list + 1 2)
> '(#<procedure:list> #<procedure:+> 1 2)

That evaluates all five expressions in the expression (namely "list", "list" again, "+", "1" and "2").  The first three evaluate to procedures, and the last two evaluate to themselves.  Then it invokes the procedure "list" (the first expression inside the parentheses, which must, as in this case, be a procedure) with the remaining (four) values as arguments.  The result, as you can see, is a list of two procedures and two numbers.

But this is a case where getting the basics right at the beginning pays dividends later.

Happy reading...

Norman


----

(By the way, Racketeers, <http://docs.racket-lang.org/r5rs-std/index.html> currently returns a 404)

-- 
Norman Gray  :  http://nxg.me.uk
SUPA School of Physics and Astronomy, University of Glasgow, UK



Posted on the users mailing list.