99 |
* Type Index:: |
* Type Index:: |
100 |
@end menu |
@end menu |
101 |
|
|
102 |
|
|
103 |
@node Jump Start |
@node Jump Start |
104 |
@chapter Jump Start |
@chapter Jump Start |
105 |
|
|
107 |
Before giving an overview of Guile, I present some simple commands and |
Before giving an overview of Guile, I present some simple commands and |
108 |
programs that you can type to get going immediately. |
programs that you can type to get going immediately. |
109 |
|
|
110 |
Start by invoking the Guile interpreter (usually you do this by just |
Start by invoking the Guile interpreter. Usually you do this by just |
111 |
typing @code{guile}). Then type (or paste) the following expressions at |
typing @code{guile}. Then type (or paste) the following expressions at |
112 |
the prompt; the interpreter's response is preceded (in this manual) by |
the prompt; the interpreter's response is preceded (in this manual) by |
113 |
@result{}. |
@result{}. |
114 |
|
|
119 |
(+ 20 35) |
(+ 20 35) |
120 |
@result{} 55 |
@result{} 55 |
121 |
(define (recursive-factorial n) |
(define (recursive-factorial n) |
122 |
(if (= n 0) |
(if (zero? n) |
123 |
1 |
1 |
124 |
(* n (recursive-factorial (- n 1))))) |
(* n (recursive-factorial (- n 1))))) |
125 |
(recursive-factorial 5) |
(recursive-factorial 5) |
126 |
@result{} 120 |
@result{} 120 |
127 |
|
(quit) |
128 |
|
@end lisp |
129 |
|
|
130 |
|
In this example we did some simple arithmetic @code{(+ 20 35)} and got |
131 |
|
the answer @code{55}. Then we coded the classic (and rather wasteful) |
132 |
|
factorial algorithm and computed the factorial of @code{55}. Finally we |
133 |
|
quit with @code{(quit)}. |
134 |
|
|
135 |
|
@cindex bignumbers |
136 |
|
We can find out about some of Scheme's nice features by asking for the |
137 |
|
factorial of some big number, say @code{500}. On some systems the |
138 |
|
correct answer will be returned (I do not indicate calling and leaving |
139 |
|
the guile session anymore). |
140 |
|
|
141 |
|
@lisp |
142 |
(recursive-factorial 500) |
(recursive-factorial 500) |
143 |
@result{} 1220136825991110068701238785423046926253574342803192842192413588 |
@result{} 1220136825991110068701238785423046926253574342803192842192413588 |
144 |
3858453731538819976054964475022032818630136164771482035841633787 |
3858453731538819976054964475022032818630136164771482035841633787 |
158 |
3896881639487469658817504506926365338175055478128640000000000000 |
3896881639487469658817504506926365338175055478128640000000000000 |
159 |
0000000000000000000000000000000000000000000000000000000000000000 |
0000000000000000000000000000000000000000000000000000000000000000 |
160 |
00000000000000000000000000000000000000000000000 |
00000000000000000000000000000000000000000000000 |
|
<control-D> |
|
161 |
@end lisp |
@end lisp |
162 |
|
|
163 |
In this example we did some simple arithmetic @code{(+ 20 35)} and got |
The result is an example of Scheme's @emph{bignumbers}. However, there |
164 |
the answer @code{55}. Then we coded the classic (and rather wasteful) |
are operating environments that provide (by default) too little stack |
165 |
factorial algorithm, and got a glimpse of Scheme's nice |
space. They will instead produce an error message like this: |
166 |
@emph{bignumbers} by asking for the factorial of 500. Then we quit |
|
167 |
with @code{(quit)}. |
@lisp |
168 |
@cindex bignumbers |
(recursive-factorial 500) |
169 |
|
@print{} |
170 |
|
ERROR: Stack overflow |
171 |
|
ABORT: (stack-overflow) |
172 |
|
@end lisp |
173 |
|
|
174 |
|
Rather than enlarging the system's stack, we can implement the algorithm |
175 |
|
such that it does not consume increasing stack space. This is called a |
176 |
|
@emph{tail recursive} implementation. The following definition is tail |
177 |
|
recursive and so should work on all systems. |
178 |
|
|
179 |
|
@lisp |
180 |
|
(define (tail-recursive-factorial n) |
181 |
|
(define (loop k l) |
182 |
|
(if (zero? k) l |
183 |
|
(loop (- k 1) (* k l)))) |
184 |
|
(loop n 1)) |
185 |
|
|
186 |
|
(tail-recursive-factorial 500) |
187 |
|
@result{} 1220136825991110068701238785423046926253574342803192842192413588 |
188 |
|
;; ... skipped |
189 |
|
@end lisp |
190 |
|
|
191 |
This is the most basic use of Guile: a simple Scheme interpreter. In |
This is the most basic use of Guile: a simple Scheme interpreter. In |
192 |
the rest of this tutorial I will show you how Guile has many facets: it |
the rest of this tutorial I will show you how Guile has many facets: it |