lilypond-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

"Structure and interpretation" of Scheme (was: Comments wanted on code h


From: Jean Abou Samra
Subject: "Structure and interpretation" of Scheme (was: Comments wanted on code highlighting in PDF output)
Date: Mon, 21 Feb 2022 22:34:10 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0



Le 21/02/2022 à 22:19, Luca Fascione a écrit :


On Mon, Feb 21, 2022 at 9:58 PM Jean Abou Samra <jean@abou-samra.fr> wrote:

Not sure what confuses you?

In TCL I got used to bare strings being values, not varnames, so I'm learning stuff again. It's just different, but in languages that in many other things are very similar.
Of course I don't find it confusing in C or python...
But I was just reading the page you suggested: as a beginner I read this example (define <http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-8.html#%_idx_190> capital-cities ' <http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-7.html#%_idx_88>((sweden . stockholm) (usa . washington) (germany . berlin) )) As an associative array, where the values are dereferenced variables, but I don't know about the keys, whether they're strings, symbols or also variables.



No, the values are also symbols, not dereferenced variables.

$ guile
GNU Guile 3.0.5.130-5a1e7
Copyright (C) 1995-2021 Free Software Foundation, Inc.

Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.

Enter `,help' for help.
scheme@(guile-user)>  (define capital-cities
                       '((sweden . stockholm)
                         (usa . washington)
                         (germany . berlin)
                        ))
scheme@(guile-user)> capital-cities
$1 = ((sweden . stockholm) (usa . washington) (germany . berlin))


'blabla is equivalent to (quote blabla) which prevents
evaluation inside blabla. When a symbol appears in code,
it dereferences a variable, unless it's inside a quote
in which case you get it as a symbol.


Say the first line, if it was python, would it be
capitalcities["sweden"] = stockholm
or
capitalcities[sweden] = stockholm
or
capitalcities['sweden'] = stockholm  # fake out a symbol with ticks, just for argument's sake

capitalcities['sweden'] = 'stockholm'


The TCL form uses parens for array keys, the setter would be

set capital-cities(sweden) $stockholm

Note this is exactly equivalent to these two:

set capital-cities("sweden") $stockholm
set capital-cities({sweden}) $stockholm

dblquotes interpolate (like in shell/perl) and curlies don't (like ticks in shell/perl).

It's easier to keep one's head straight given there's so little extra typing to guide you while one is still learning.


I know nothing about Tcl, but in Scheme there is no
difference between dereferencing a variable and dereferencing
a "command". An unquoted symbol that happens to be evaluated
deferences a variable, that's all. In this sense, I guess
you could think of it as if your Tcl code read

$set capital-cities("sweden") $stockholm

with $ in front of set, because set! is really a variable
that has a value in the same namespace as any other variable
(OK, it won't work if you write it as-is in Guile >= 2 due
to the way that has evolved, but set! still has a value
you can retrieve with (module-ref (current-module) 'set!) .)

Yes, I know, (set! x ...) does not evaluate x, but this
is because set! is a special macro that does special
things with its arguments. In other words, Scheme thinks
differently from Tcl in that in Tcl you always have the
$x syntax to dereference a variable, whereas in Scheme
the interpretation of a symbol is always subject to
what the surrounding context wants to do with it.
The default is to dereference a symbol. Unless quoted,
or under set!, or define, or such.


I'll learn, I'm just not there yet.

    It's the same as in most
    other languages (such as C++): a bare name dereferences
    a variable. The exception to this is within quotes,
    which prevent evaluation of symbols, returning them naked.


That too, interpolating into strings, like you have in TCL/perl/shell ("Hello $username" kinda stuff) is handy :-)


(format #t "Hello ~a" username)

?






reply via email to

[Prev in Thread] Current Thread [Next in Thread]