This is an informal treatment of Scheme syntax and grammar. For a more formal treatment, in the standard notation computer scientists use for discussing language syntax, see Syntax and Semantics in the How to Design Programs textbook.
(+ 1 2)
(* (+ 3 4) 5)
(= x y)
false, depending on whether those two values are equal.
(+ 1 2 3 4 5).)
(1 + 2)
+ 1 2
((+ 1 2))
(define variable-name value)
(define bignum 1234567890)
bignum, so that subsequently...
(* bignum bignum bignum)
(define (function-name parameter parameter ... parameter) body-expression)adds a function to the language. Each parameter is a variable name, and normally these variable names are used inside the expression.
(define (square n) (* n n))
(define (square n) (* n n))produces no result at all, but has the side effect of defining a new function named
square, taking one parameter (which it refers to internally as
(square n)", decides that the parameter n is supposed to be 5 for now, replaces all the "
n"'s in the function body with 5, producing "
(* 5 5)", and further evaluates this to get 25.
square" function, the parameter name "
n" is introduced in the header and used in the body, but then it disappears; if you were then to type
(square n)Scheme would complain that "
n" was undefined. The only way to use the "
square" function is to provide a number (or an expression whose value is a number) to match the "n" in the pattern, e.g.
(square 3 4)
squarefunction only takes one parameter.
(define (square) (* n n))
(define (square n))
(define (square n) (* n n)
(define (square n) (+ n n))
(square 3)you would get the answer 6 rather than the correct 9. This illustrates the difference between a program being legal and being correct.
(cond [question1 answer1] [question2 answer2] ... [questionn answern])returns the value of one of answer1, answer2, ... answern, depending on which question is the first to have the value
true. If question1 evaluates to
true, it returns answer1; if not, it ignores answer1 and continues with the rest of the list of question-answer pairs.
The keyword "
else" may appear in place of
questionn to indicate that the cond should return the
value of answern if no previous question was
Another way to look at it is that
(cond [true answer1] [question2 answer2] ... [questionn answern])simplifies to answer1, while
(cond [false answer1] [question2 answer2] ... [questionn answern])simplifies to
(cond [question2 answer2] ... [questionn answern])
(cond [(> grade 90) 'A] [(> grade 80) 'B] [(> grade 70) 'C] [(> grade 60) (warn 'D)] [else (warn 'F)])
(cond (< bignum 4) 5 (>= bignum 4) 6)because the question and answer in each clause must be surrounded by square brackets; this person probably meant
(cond [(< bignum 4) 5] [(>= bignum 4) 6])
(cond [(+ bignum 4) 5] [(- bignum 4) 6])
because the question must return a boolean (the expressions "
(+ bignum 4)" and "
4)" both return numbers instead)
(cond [(< bignum 4) 5) ((>= bignum 4) 6]
because the parentheses and brackets aren't matched. The parentheses in
(< bignum 4) match
nicely, but outside this are a left-bracket and a
right-parenthesis, which don't match. Similarly,
(>= bignum 4) is matched, but outside it are a
left-parenthesis and a right-bracket, which don't
match. Finally, there is no right-parenthesis to match
the one before
(define-struct structure-name (field1 field2 ... fieldn))
(make-structure-name value1 value2 ... valuen)takes n objects and returns a structure-name.
(structure-name? object)takes an object and returns a boolean indicating whether the object is a structure-name.
(structure-name-field1 struct)takes a structure-name and returns the field1 part of it.
(structure-name-fieldn struct)takes a structure-name and returns the fieldn part of it.
; A student is a symbol (name), a number (GPA), ; a symbol (class, e.g. 'freshman, 'sophomore, etc.), ; and a number (ID). (define-struct student (name GPA class ID)) ; make-student: symbol number symbol number => student ; student?: object => boolean ; student-name: student => symbol ; student-GPA: student => number ; student-class: student => symbol ; student-ID: student => numberThe one line of actual code in this example tells Scheme to add a new data structure to the language, along with its constructor (
make-student), discriminator (
student?), and slot-access functions (
(make-student 'Joe-Schmoe 2.6 'freshman 123456789)creates and returns a new "student" object with the specified properties.
(define my-student (make-student 'Jane-Doe 3.3 'sophomore 987654321))creates a new "student" object and stores it under the new name "
(student? (make-student 'Bill 2.8 'junior 918273645))
make-student, student?, student-name, etc. explicitly; they are defined automatically when you say
(define-struct student (name GPA class ID)).