Up: Scheme tutorials [Contents][Index]
Guix uses the Guile implementation of Scheme. To start playing with the
language, install it with guix install guile
and start a
REPL—short for read-eval-print loop—by running guile
from the command line.
Alternatively you can also run guix environment --ad-hoc guile -- guile
if you’d rather not have Guile installed in your user profile.
In the following examples, lines show what you would type at the REPL; lines starting with “⇒” show evaluation results, while lines starting with “-|” show things that get printed. See Using Guile Interactively in GNU Guile Reference Manual, for more details on the REPL.
#true
and #false
(abbreviated #t
and #f
) stand for the
Booleans “true” and “false”, respectively.
Examples of valid expressions:
"Hello World!" ⇒ "Hello World!" 17 ⇒ 17 (display (string-append "Hello " "Guix" "\n")) -| Hello Guix! ⇒ #<unspecified>
lambda
term:
(lambda (x) (* x x)) ⇒ #<procedure 120e348 at <unknown port>:24:0 (x)>
The above procedure returns the square of its argument. Since everything is
an expression, the lambda
expression returns an anonymous procedure,
which can in turn be applied to an argument:
((lambda (x) (* x x)) 3) ⇒ 9
define
:
(define a 3) (define square (lambda (x) (* x x))) (square a) ⇒ 9
(define (square x) (* x x))
list
procedure:
(list 2 a 5 7) ⇒ (2 3 5 7)
'(display (string-append "Hello " "Guix" "\n")) ⇒ (display (string-append "Hello " "Guix" "\n")) '(2 a 5 7) ⇒ (2 a 5 7)
`(2 a 5 7 (2 ,a 5 ,(+ a 4))) ⇒ (2 a 5 7 (2 3 5 7))
Note that the above result is a list of mixed elements: numbers, symbols (here
a
) and the last element is a list itself.
let
(see Local
Bindings in GNU Guile Reference Manual):
(define x 10) (let ((x 2) (y 3)) (list x y)) ⇒ (2 3) x ⇒ 10 y error→ In procedure module-lookup: Unbound variable: y
Use let*
to allow later variable declarations to refer to earlier
definitions.
(let* ((x 2) (y (* x 3))) (list x y)) ⇒ (2 6)
#:
(hash, colon) followed by
alphanumeric characters: #:like-this
.
See Keywords in GNU Guile Reference Manual.
%
is typically used for read-only global variables in
the build stage. Note that it is merely a convention, like _
in C.
Scheme treats %
exactly the same as any other letter.
define-module
(see Creating Guile
Modules in GNU Guile Reference Manual). For instance
(define-module (guix build-system ruby) #:use-module (guix store) #:export (ruby-build ruby-build-system))
defines the module guix build-system ruby
which must be located in
guix/build-system/ruby.scm somewhere in the Guile load path. It
depends on the (guix store)
module and it exports two variables,
ruby-build
and ruby-build-system
.
For a more detailed introduction, check out Scheme at a Glance, by Steve Litt.
One of the reference Scheme books is the seminal “Structure and
Interpretation of Computer Programs”, by Harold Abelson and Gerald Jay
Sussman, with Julie Sussman. You’ll find a
free copy
online, together with
videos of the lectures by the authors. The book is available in Texinfo
format as the sicp
Guix package. Go ahead, run guix install
sicp
and start reading with info sicp
(see Structure and Interpretation of Computer Programs).
An unofficial ebook is also
available.
You’ll find more books, tutorials and other resources at https://schemers.org/.
Up: Scheme tutorials [Contents][Index]