Introduction to Scheme II:
Basic Concepts

References

  • Index of JLIB Primitives.
  • Index of Scheme Primitives.
  • The JScheme homepage
  • The Scheme Homepage
  • The R4RS Scheme Manual


    Scheme Expressions and Java Values

    The Scheme interpreter can be viewed as a kind of symbolic calculator that allows you to interactively evaluate expressions to get values. In this section we explore this view of Scheme as a fancy calculator. In particular, we examine the kinds of objects that Scheme is able to manipulate, the operations it is able to perform, and the way in which you can use the "define" expression to name new objects, and the "lambda" expression to create functions. You can try out the scheme code using the interpreter applet below (which detaches from the page for those who want it in a separate window).



    Constants


    Scheme allows you to work with a variety of values. We provide a brief overview here. For more details, you can look over the full Jscheme documentation for constants. . There are several basic types of values that Jscheme manipulates: numbers, characters, strings, symbols, booleans, and Java objects. Each of which is described below. are


    JLIB

    The version of Scheme we use in this class differs from most other implementations of Scheme in that it has been written in Java (which is why it can be run directly from your browser) and it provides access to a fairly large set of Java primitives which are not part of standard Scheme. It also provides a library of graphical user interface building tools. This library is called JLIB and has been written in Jscheme. Click here for a list of the JLIB primitives.

    Recall, for example, the Hello.scm program we saw in the previous lecture:
        (define win 
           (window "Hello.scm"
              (label "HELLO WORLD!" red (CourierBold 60))))
        (.pack win)
        (.show win)
    
    This uses six Jlib primitives:

    This program also uses two integer constants (100, 200) and two string constants ("First Example" and "Hello World").


    Variables and Definitions

    Scheme allows you to give store values in variables using the define statement. For example, to store the value 3.1415926 in the variable pi, you would evaluate the following expression:
      (define pi 3.1415926)
    
    If we then evaluate the symbol 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 
    
    It would be clearer if we used "function" instead of "lambda"
      (define square (function (x) (* x x)))  ;; this is not valid scheme!!!
    
    since that is the intended meaning, but the use of lambda has a long history which we honor by using it to define functions. An alternate approach to defining functions uses the following syntax in which the fact that "square" is a function definition is indicated by the presence of the parentheses around the function name and its argument: "square x".
      (define (square x) (* x x)) ; -->   square
      (square (square (square 2))) ; -->  (square (square 4)) --> (square 16) --> 256
    
    Functions involving several variables can be just as easily defined
      (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
    


    Scheme Syntax

    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.

    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.

    We now preview some of the most important special forms: