Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Scheme (programming language)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Scheme (programming language)
(section)
Add topic