Introduction to Scheme PDF

Title Introduction to Scheme
Course JVM Programming
Institution University of Greenwich
Pages 3
File Size 97.9 KB
File Type PDF
Total Downloads 1
Total Views 128

Summary

Download Introduction to Scheme PDF


Description

Introduction to Scheme: Scheme uses a Read-Eval-Print loop:

In Scheme, numbers are expressions known as primitive expressions. Other expressions are compound (or combination) expressions. Compound expressions always have the form:

Scheme uses prefix notation, for example: (+ 7 6) gives 13. Expressions can be also be nested. 2 + 3 * 4 is equivalent to (+ 2 (* 3 4)) in Scheme. The value of a compound expression is the result of applying the procedure specified by the operator to the operands. To avoid typing out long expressions multiple times when applying different procedures to it, you can use definitions, for example: (define name value). This

 Creates a new variable - allocates a memory location where values are stored.  Gives the location a name and links the name to that variable.  Puts the value in that location. Variables are also primitive expressions, but they can be procedures. Scheme stores the variables in a table of names and values called the environment. Evaluation is recursive. To evaluate a compound expressions: 1. Evaluate each sub-expression 2. Apply the values of the first sub-expression to the latter. The first value of an expressions must be a procedure. Procedures and Abstraction Procedures are defined using lambda. The value of (lambda) is the procedure, which when applied to a list of operand values evaluates body with substitutions for the corresponding parameters. (lambda (param1 param2 ... paramn) (body) ) The params are zero or more parameters. body is an expression which refers to the parameters. An alternative is: (define (name param1 param2 ... paramn) (body) ). There are many different ways of defining equivalent procedures, so what is the best way to do so? Not using abstraction. Abstraction is the generalisation of a method, e.g., defining double and then using that rather than (* 2 6). Abstraction is our fundamental design methodology. What Happens During Evaluation? Take for example this code: (define (double x) (* 2 x)) (define (triple x) (+ x (double x))) Scheme uses the substitution model for evaluation, so evaluating some simple code goes like this: (triple 5)

(+ 5 (double 5)) (+ 5 (*2 5)) (+ 5 10) 15 Choice either/or You can do either/or choices like so: (if expression then else). expression is evaluated to true or false. then is the value of the if expression if the expression is true else is the value of the if expression if the expression is false. #f is false, and #t is true, however anything that isn't #f is considered to be true. To add more power to choice, you can use logical composition operations. (and expr1 expr2 ... exprn) This is evaluated left to right, until one is false or all are true. This is different to the standard Scheme operation. (or expr1 expr2 ... exprn) This also evaluates left to right, until one is true or all are false. (not expr) This returns the opposite value to expr. One of several (cond (pred1 expr1) (pred2 expr2) ... (predn exprn) ) The value of (cond) is the value of the first expression whose predicate evaluates to true. If the predicate is #t, this is always true and is a useful "catch-all" situation....


Similar Free PDFs