Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Scheme (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Block structure=== Scheme inherits its block structure from earlier block structured languages, particularly [[ALGOL]]. In Scheme, blocks are implemented by three ''binding constructs'': [[Let expression|<code>let</code>]], <code>let*</code> and <code>letrec</code>. For instance, the following construct creates a [[Block (programming)|block]] in which a symbol called <code>var</code> is bound to the number 10: <syntaxhighlight lang="Scheme"> (define var "goose") ;; Any reference to var here will be bound to "goose" (let ((var 10)) ;; statements go here. Any reference to var here will be bound to 10. ) ;; Any reference to var here will be bound to "goose" </syntaxhighlight> Blocks can be [[Nesting (computing)|nested]] to create arbitrarily complex block structures according to the need of the programmer. The use of block structuring to create local bindings alleviates the risk of [[Naming collision|namespace collision]] that can otherwise occur. One variant of <code>let</code>, <code>let*</code>, permits bindings to refer to variables defined earlier in the same construct, thus: <syntaxhighlight lang="Scheme"> (let* ((var1 10) (var2 (+ var1 12))) ;; But the definition of var1 could not refer to var2 ) </syntaxhighlight> The other variant, <code>letrec</code>, is designed to enable [[mutual recursion|mutually recursive]] procedures to be bound to one another. <syntaxhighlight lang="Scheme"> ;; Calculation of Hofstadter's male and female sequences as a list of pairs (define (hofstadter-male-female n) (letrec ((female (lambda (n) (if (= n 0) 1 (- n (male (female (- n 1))))))) (male (lambda (n) (if (= n 0) 0 (- n (female (male (- n 1)))))))) (let loop ((i 0)) (if (> i n) '() (cons (cons (female i) (male i)) (loop (+ i 1))))))) (hofstadter-male-female 8) ===> ((1 . 0) (1 . 0) (2 . 1) (2 . 2) (3 . 2) (3 . 3) (4 . 4) (5 . 4) (5 . 5)) </syntaxhighlight> (See [[Hofstadter sequence#Hofstadter Female and Male sequences|Hofstadter's male and female sequences]] for the definitions used in this example.) All procedures bound in a single <code>letrec</code> may refer to one another by name, as well as to values of variables defined earlier in the same <code>letrec</code>, but they may not refer to ''values'' defined later in the same <code>letrec</code>. A variant of <code>let</code>, the "named let" form, has an identifier after the <code>let</code> keyword. This binds the let variables to the argument of a procedure whose name is the given identifier and whose body is the body of the let form. The body may be repeated as desired by calling the procedure. The named let is widely used to implement iteration. Example: a simple counter <syntaxhighlight lang="Scheme"> (let loop ((n 1)) (if (> n 10) '() (cons n (loop (+ n 1))))) ===> (1 2 3 4 5 6 7 8 9 10) </syntaxhighlight> Like any procedure in Scheme, the procedure created in the named let is a first-class object.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)