[2/1/99] =============================================================================== * Note: these section notes will be available on the web. In most cases, the time will probably be too short to cover all, so some things will get to the next section, but it is always useful to look at all of the examples. =============================================================================== * There is third section group opened, meeting on the same days, 7:30pm. =============================================================================== * Newsgroup - people do not use it enough. Also, should mail eli@cs.cornell.edu to get a UID number, it will be impossible to submit homeworks without it. Notes about submissions: it is for the convenience of both staff and students. Any attempt to play with it (even an unsuccessful one) will be severely punished. =============================================================================== * Reminder - the parens can be compared to C parens - they mean apply something - this is the reason why (+ (1) (2)) won't work - if you use C syntax that is "+(1(), 2())" but "1" isn't a function so "1()" is an error. =============================================================================== * Go over the difference between "1", "(lambda () 1)" and "((lambda () 1))". Also note that lambda is a special form - the reson that "(lambda () (/ 1 0))" works and "((lambda () (/ 1 0)))" don't (saying "I'm going to kill you" is ok, but doing it...). =============================================================================== * Again - the important difference between _syntax_ and _semantics_ - a good way to think about this is the difference between the string "42" stored in a file somewhere (two ASCII values), and the number "42" stored in memory (in some representation). You could also continue with the above example - there is nothing wrong with "murder" - it's just a word, but murder is something you'll go to jail for. The evaluation function that Scheme uses is actually a function that takes a piece of syntax and returns its semantics. =============================================================================== * Define is used for definitions, do not use them for changing values, for example, it is *extremely* ugly to write (define x (+ x 1)), and not guaranteed to work. =============================================================================== * There are two boolean values built in Scheme - #t (true) and #f (false). They can be used in if statements, for example: -------------------- (if (< 2 3) 10 20) --> 20 -------------------- because (< 2 3) evaluates to #t. As a matter of fact, *any* value except for #f is considered to be true, so: -------------------- (if 0 1 2) --> 1 (if "false" 1 2) --> 1 -------------------- =============================================================================== * Note: Scheme is a _functional_ language - so _everything_ has a value - this makes the expression -------------------- (if test consequent) -------------------- have no meaning when "test" will evaluates to #f. This is unlike Pascal/C where statements _do_ something (side effect) like printing or an assignment - here an if statement with no alternate part will just "do nothing" if the test is false... Scheme, however, must return some value - our implementation could decide on simply returning #f as the value of -------------------- (if #f something) -------------------- but this is something you shouldn't rely on (as all other "it-will-probably-do-this" things), and, in MzScheme it returns something different. =============================================================================== * Example for using an if statement, and a recursive function: -------------------- (define fact (lambda (n) (if (zero? n) 1 (* n (fact (- n 1)))))) -------------------- =============================================================================== * Aside: there a "syntactic sugar" that can be used in Scheme - using -------------------- (define (f ...) ...) -------------------- is sort of an abbreviation for -------------------- (define f (lambda (...) ...)) -------------------- so the fact function cane e written as: -------------------- (define (fact n) (if (zero? n) 1 (* n (fact (- n 1))))) -------------------- ===============================================================================