We will use Dr Scheme in this class. See the previous page for the link to download Dr Scheme. If you are using SCHEME in the Wright Hall labs, use PLT Scheme. Do not use MIT Scheme. --- Dr. Scheme is under PLT submenu. PLT SCHEME. REPL = Read, Evaluate, Print Loop Top window is Definition window Bottom window is Interactions window > (+ 2 4) (operator operand operand) 6 > (+ 2 4 100 200) Read the application, evaluate the procedure call to + 306 Print value of application > (expt 2 10) 10 1024 2 = 1024 > (expt 2 4) 2 to the 4th power = 16 16 5 > (+ (expt 2 5) 1)) What is the value of 2 + 1 33 > (+ (* 2 5) (- 20 17)) What is the value of (2 * 5) + (20 - 17) 13 10 + 3 LIST PROCESSING = LISP Scheme is a dialect of LISP --- - > (2 3 4) Oh oh! 2 is NOT an operator! procedure application: expected procedure, given: 2; arguments were: 3 4 This error message makes sense. (operator operand1 operand2 ...) is the syntax for applications. The ()s in Scheme indicate to the Scheme interpretor REPL loop, that it is to EVALUATE the application, i.e. to assume 2 is a procedure (operation or operator) to be applied to 3 and 4, the operands. > '(2 3 4) (list 2 3 4) The ' (single quote symbol) indicates to Scheme that (2 3 4) is a literal, i.e. a LIST, so it does not treat it as an application. > (car '(2 3 4)) 2 > (cdr '(2 3 4)) (3 4) > (first '(22 33 44)) 22 > (rest '(22 33 44)) (33 44) > (first (rest '(22 33 44))) Getting the 2nd element 33 Note: (car (cdr '(22 33 44))) would also return 33 car = first cdr = rest
Syntax for defining a function: (define (function-name function-parameters) BODY ) Example: (define (volume height width length) TOP Window is usually (* height width length)) used to DEFINE functions. > (volume 10 20 30) WARNING: Interactions window is out of sync with the definitions window. Click Execute. reference to undefined identifier: volume > AFTER Clicking the EXECUTE button to make the new definition of the volume procedure available for use in the INTERACTIONS window, and then pressing escape key, followed by P to bring up the Previous interaction, it works! > (volume 10 20 30) 6000 > For Wednesday, think about how you would implement a distance function as a Scheme definition: > (distance '(0 0) '(3 4)) 5 > (distance '(-1 2) '(4 3)) #i5.0990195135927845 > NOTE: You will need to use car (or first), cdr(or rest), sqrt, expt, + and finally - operations. Here are some helful hints and illustrations related to finding the distance between point p1 = (0 0) and point p2 = (3 4) > (sqrt 25) 5 > (sqrt (+ 9 16)) 5 > (sqrt (+ (expt 3 2) (expt 4 2))) 5 > (distance '(0 0) '(3 4)) Note: x1 - x2 for p1 = (0 0) = (x1 y1) 5 and p2 = (3 4) = (x2 y2) is -3 y1 - y2 would be -4 Therefore, the following more closely illustrates the example: > (sqrt (+ (expt -3 2) (expt -4 2))) 5 > (distance '(0 0) '(3 4)) 5
There may be some links up here before Wednesday's class, as there are lots of Scheme resources on the web page to read.