Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scheme: Basic Functionality

Similar presentations


Presentation on theme: "Scheme: Basic Functionality"— Presentation transcript:

1 Scheme: Basic Functionality
S-expressions Conses Lists Predicates Evaluation and quoting Conditional Evaluation CSE S. Tanimoto Scheme's Basic Functionality

2 Scheme S-Expressions: ATOMs
Every Scheme object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE, A-SYMBOL 1, 5.7, 3/5 Many other Scheme data objects are considered to be atoms (even strings and arrays are atoms!). CSE S. Tanimoto Scheme's Basic Functionality

3 Lisp S-Expressions: CONSes
Every Lisp object is either an ATOM or a CONS A CONS represents an association or pairing of two other Scheme objects. (A . B) (APPLE . RED) (PI ) (X . (Y . Z)) CSE S. Tanimoto Scheme's Basic Functionality

4 Scheme S-Expressions: Lists
We define lists as follows: The empty list is a list. It is written as ( ) Any cons having the following structure is a list, (S1 . (S2 . ( (Sn . ( ) ) ... ) ) ) where each Si is either an atom or a cons. This list is written in list notation as (S1 S2 … Sn) CSE S. Tanimoto Scheme's Basic Functionality

5 CSE 341 -- S. Tanimoto Scheme's Basic Functionality
Examples of Lists > ’(a b c d e) (A B C D E) > () () > ’() > ’(apple . (banana . (lime . ( )))) (APPLE BANANA LIME) CSE S. Tanimoto Scheme's Basic Functionality

6 Using LIST? To Identify Lists
> (list? ’(a b c)) #t > (list? ’x) #f > (list? ’(a b c . d)) > (list? () ) CSE S. Tanimoto Scheme's Basic Functionality

7 Scheme Tries to Print Conses as Lists
> ’(a . (b . c)) (A B . C) > ’(a . () ) (A) > ’((a . b) . (c . d)) ((A . B)C . D) > ’((() . ()) . (() . ())) ((())()) CSE S. Tanimoto Scheme's Basic Functionality

8 CSE 341 -- S. Tanimoto Scheme's Basic Functionality
Scheme Forms A form is a list that can be evaluated by Scheme. If the first element names a procedure, then the form is a procedural form. > ( ) ; a procedural form 6 > (procedure? +) #t > (define x 5) ; a special form > (procedure? define) Error! CSE S. Tanimoto Scheme's Basic Functionality

9 Evaluation of Procedural Forms
A procedural form is evaluted as follows: If there are any arguments in the form, then they are evaluated in left-to-right order. The number of arguments is compared with the number permitted by the procedure named by the first element of the form. If the number is compatible, then the procedure is applied to the values of the arguments. CSE S. Tanimoto Scheme's Basic Functionality

10 CSE 341 -- S. Tanimoto Scheme's Basic Functionality
QUOTE Unlike procedural forms, special forms are not required to have their arguments evaluated. QUOTE is a special form that returns its argument unevaluated. > (quote ( )) ( ) > (quote x) X > ’( ) > ’x CSE S. Tanimoto Scheme's Basic Functionality

11 CSE 341 -- S. Tanimoto Scheme's Basic Functionality
SET! SET! is a special form that evaluates its second argument and assigns that value to the defined variable which must be its first argument. > (define x 0) > (set! x ( )) > x 6 > (define y 0) > (set! y ’( )) > y ( ) > (set! y (rest y)) > (set! y (first y)) 1 CSE S. Tanimoto Scheme's Basic Functionality

12 CSE 341 -- S. Tanimoto Scheme's Basic Functionality
IF IF is a special form that evaluates its first argument. If the result is #f it skips its second argument and evaluates and returns its third element if any. If result of evaluating the first element was not #f, it evaluates the second argument and returns that. > (define x 10) 10 > (if (> x 2) (- x 1) (+ x 1)) 9 CSE S. Tanimoto Scheme's Basic Functionality


Download ppt "Scheme: Basic Functionality"

Similar presentations


Ads by Google