Scheme (programming language): Difference between revisions

Content deleted Content added
No edit summary
Ap (talk | contribs)
mNo edit summary
Line 29:
 
== Language Elements ==
 
=== Comments ===
 
Comments are preceded by a [[semi-colon]] (;) and continue for the rest of the line.
 
=== Variables ===
Line 136 ⟶ 140:
 
(factorial 5)
;; => 120
 
or a higher order function like ''map'' which applies a function to every element of a list
Line 146 ⟶ 150:
 
(map (lambda (x) (* x x)) '(1 2 3 4))
;; => (1 4 9 16)
 
Another way to define loops is the ''named'' <code>named let</code> statement and the <code>do</code> statement. To use factorial and map again to illustrate both forms:
 
(define (factorial n)
Line 157 ⟶ 161:
 
(factorial 5)
;; => 120
 
 
Line 166 ⟶ 170:
 
(map (lambda (x) (* x x)) '(1 2 3 4))
;; => (1 4 9 16)
 
Please note that in both cases, the tail recursive version is preferrable.
 
=== Input/output ===
 
Scheme has the concept of ''ports'' to read from or to write to. Scheme defines three default ports, accessible with the functions: <code>current-input-port</code>, <code>current-output-port</code> and <code>current-error-port</code>.