Scheme (programming language): Difference between revisions

Content deleted Content added
UserGoogol (talk | contribs)
No edit summary
fix #t vs "true" distinction; partially fix tail recursion exposition
Line 115:
(else exprn))
 
The first expression for which the test evaluates to true (anything other than #tf counts as true) will be evaluated. If notall test resultsresult in #tf, the else clause is evaluated.
 
A variant of the cond clause is
Line 123:
...)
 
In this case, expr should beevaluate to a function that takes one argument. If test evaluates to #ttrue, the function is called with the return value of test.
 
Scheme also has
Line 133:
==== Loops ====
 
Loops in Scheme usually take the form of tail recursion. A classical example is the factorial function, which can be defined non-tail-recursively:
 
(define (factorial n)
Line 142:
;; => 120
 
or a higher order function like ''map'' which applies a function to every element of a list, and can be defined non-tail-recursively:
 
(define (map f lst)
Line 152:
;; => (1 4 9 16)
 
AnotherWe way tocan define loopsboth isof thethese trail recursively as follows. The ''named'' <code>let</code> statement and the <code>do</code> statement are syntactic sugar which simpify tail recursive definitions. To use factorial and map again to illustrate both forms:
 
(define (factorial n)
Line 172:
;; => (1 4 9 16)
 
Please note that in both cases, the tail recursive version is preferrable due to its decreased use of space.
 
=== Input/output ===