1729 ; --> 1729 0.00390625 ; --> 0.00390625 |
2.7182817284590452353602874713526624977 ; --> 2.7182817284590453 |
10000000.0 ; --> 1.0E7 |
#'a' ; --> a |
#'\uXXXX'
.
"Hello World" "result" "window" "1789"
The Scheme interpreter will simply echo these back to you:
"This sentence is false" ; --> "This sentence is false" |
#t
for true and
#f
for false.
#t ; --> #t |
'window ; --> window 'value5 ; --> value5 '@*&!$ ; --> @*&!$ + ; --> {silk.Primitive +[0,n]} sin ; --> {silk.Primitive sin[1]} |
+ - * / log exp sin cos tan ...
(+ 1 2 3 4 5) ; --> 15 (cos (acos 1.0)) ; --> 1.0 (exp (log (exp 1))) ; --> 2.718281828459045 (string-append "This sentence" " is false") ; --> "This sentence is false" (not #t) ; --> #f |
(define b (java.awt.Button. "Do Not Push")) ;; create a button (define w (java.awt.Frame. "WARNING")) ;; create a window (define c (java.awt.Color. 255 200 145)) ;; create a color (.add w b) ;; add button to window (.setBackground w c) ;; change color of window to c (.pack w) (.show w) ;; select best window size at make it appear (define tento25 ;; create a big whole number (java.math.BigInteger. "10000000000000000000000000")) (define google (.pow tento25 4)) ;; raise it to the fourth power |
For a complete list of the Java 1.3 Standard Edition primitives (all of which are directly accessible from Jscheme), see the index of all Java 1.3 (Standard Edition) members of the 1840 Java classes. For a precise explanation of the Jscheme syntax for Java literals see the Java Literals page of the Jscheme documentation.
Recall, for example, the Hello.scm program we saw in the previous lecture:
This uses six Jlib primitives:
(define win
(window "Hello.scm"
(label "HELLO WORLD!" red (CourierBold 60))))
(.pack win)
(.show win)
(window TITLE C)
(label TEXT COLOR FONT)
(CourierBold N)
-- create a bold face Courier font of size N.
red
-- the value of this symbol is the color "red"
(.pack WINDOW)
-- resize the window so it is as small as possible,
but so that all components it contains are visible.
(.show COMPONENT)
pi
, you
would evaluate the following expression:
(define pi 3.1415926) |
pi
, the interpreter will write 3.1415926
in the Results textarea. We can also define new functions. For example, to define
a square function you can use
(define square (lambda (x) (* x x))) ; --> square (square 55) ; --> 3025 |
(define square (function (x) (* x x))) ;; this is not valid scheme!!! |
(define (square x) (* x x)) ; --> square (square (square (square 2))) ; --> (square (square 4)) --> (square 16) --> 256 |
(define (area width height) (* width height)) ; --> area (area 9 11) ; --> 99 (define volume (lambda (width height depth) (* width height depth))) ; --> volume (volume 2 3 4) ; --> 24 |
Syntax refers to the grammatical rules whereby one combines elements of a language to create valid expressions in that language. Scheme has relatively few syntactic rules.
We have seen two examples of scheme syntax so far. The main one is the application which
allows us to apply functions to arguments:
where F is an expression that evaluates to a function, and A, B, ..., C are expressions that
evaluate to some values, and the function is then applied to those values.
(F A B ... C)
There are about a dozen additional syntactic forms, which collectively are called
the special forms. We have seen one already, the definition expression:
This expression evaluates the expression EXPR to get a value which it then assigns to the
variable NAME.
(define NAME EXPR)
We now preview some of the most important special forms:
( if TEST THEN ELSE)To evaluate this expression, we first evaluate the expression TEST. If it returns #t (the true value) then we evaluate the THEN expresssion, if it returns #f (the false value), then we evaluate the ELSE expression. For example, to evaluate the following expression
(define dob 1982) (if (< (- 2000 dob) 21) "minors may not drink!!" "want a beer??") |
(- 2000 dob)
which returns 18, and then evaluate
(< 18 21)
which returns #t because 18 is less than 21.
Since the test was true, we evaluate the THEN expression, which in this case is just the
string "minors may not drink".
(cond (Test1 value1) (Test2 Value2) ... (TestN valueN) (else defaultValue))This expression is a generalized "if" statement. It applies several tests and returns the value corresponding to the first test which is not false. If all tests are false then the default value, specified by the final "else" clause, is used.
(let ((Name1 value1) (Name2 Value2) ... (NameN valueN)) Body)This expression computes the names to the values and then uses these bindings to evaluate the body, which can consist of several expressions.