Scheme doesn't care whether you put a whole expression on one line or on several, but if you choose to break an expression over several lines, DrScheme will help you see its structure by indenting.
Examples:
(define(*7 x)(* 7 x))has only the spaces that are absolutely necessary. A somewhat more readable version is ...
(define (*7 x) (* 7 x))Even better, ...
(define (*7 x) (* 7 x))illustrates both the Scheme programmers' convention of putting the function header on one line and the body on the next one or more lines, and DrScheme's automatic indentation.
( define ( *7 x ) ( * 7 x) )still defines exactly the same function, although it has so many spaces and line breaks that it's hard to read.
Notes: If DrScheme indents things in a way you don't think is correct, it probably means you've got one too many or one too few parentheses on the previous line. If you've changed part of a line, it's often advisable to press the TAB key, which tells DrScheme to re-indent this line in accordance with the parenthesis nesting. If the indentation is still "incorrect", don't change the indentation; fix the parentheses. Changing the indentation alone won't make the program correct; it'll only make it look correct to a human being.
Examples:
(+ 3 (* 4 5))
(+ 3 (* 4 5)
(cond [(<= n 0) 1] [else (* n x)])is legal. Note that by convention, we use square brackets around each clause of a
cond
;
parentheses or braces would also be legal.
(cond ([<= n 0) 1] [else [* n x]))is illegal because the left-bracket before
<=
n 0
doesn't match the right-parenthesis after
it, and the left-parenthesis before [<= n 0)
1
doesn't match the right-bracket after it, and
the left-bracket before else [* n x]
doesn't match the right-parenthesis after it.
Note: When you type a right parenthesis, bracket, or brace, DrScheme will automatically figure out what it's supposed to match, change it to the appropriate punctuation mark, and shade everything in between. If you were expecting to match a parenthesis and DrScheme puts in a bracket, or if the shaded portion isn't what you expected, you've got a mismatch somewhere in between.
Examples:
+
sqrt
square
true
my age
my-age
my_age
myAge
define
cond
, else
,
and define-struct
.arr[x]
*7
+7
; This is a comment that takes up the whole line.
(define bignum 1234567890);a really big number
(define biggernum 1234578901234567890) ; a really really
really big number
Example:
908723609587129087602439873245
Examples:
2390874/132948576
2390874 /132948576
2390874
(a perfectly good integer) and
/132948576
(a presumably-undefined variable
name).2390874 / 132948576
2390874
, the predefined
function-name /
, and the number
132948576
.Examples:
3.1415926
6.02e23
-.738E-17
Notes: Such numbers are usually
inexact: for example,
(* 49.0 (/ 1.0 49.0))
will
probably
be very close, but not exactly equal, to 1.0. They are
limited in precision -- only about fifteen digits after
the decimal point, and the exponent can be no larger
than about 308 (depending on what's in front of the
decimal point)
"This is a \"string\" containing quotation marks."If you want to put a backslash inside a string, precede it with a backslash, e.g.
"This is another string containing a single \\ backslash."
Since line breaks are allowed inside a string, if you forget the closing quote Scheme will let you go on to the next line and the next, patiently waiting for you to end the string.
(cond [(> grade 90) "A"] [(> grade 80) "B"] [(> grade 70) "C] [(> grade 60) "D"] [else "F"]) ] ] ]]]]]]]]By the unexpected indentation, one could tell something was funny here. Because the quotation mark after C was omitted, everything from there to just before D is treated as a single string; D is treated as a variable name, followed by a string running to just before F, then the variable name F, then a string that still hasn't ended (no matter how many close-brackets you put in).
Examples:
'bluebird
'+
'my age
Notes:
'bluebird
and the variable-name
bluebird
, or between the symbol '+
and the predefined function-name +
.Examples of use:
'bluebird
(should return exactly the
same thing)(define this-bird 'bluebird)
this-bird
(should return
'bluebird)(symbol=? this-bird 'crow)
(should return
false)(symbol=? this-bird 'bluebird)
(should
return true)(symbol=? this-bird bluebird)
(should produce an error message because the variable
"bluebird" is undefined; note that since it doesn't start
with an apostrophe, Scheme treats it as a variable rather
than a symbol)