30-Introduction-To-Scheme - CS107 Spring 2007 Handout 30...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS107 Handout 30 Spring 2007 May 16, 2007 Scheme Basics Much of this handout was leveraged off of some LISP handouts written by Todd Feldman and Nick Parlante. Getting Started We’ll be relying on an open-source application called Kawa to write and test our Scheme code. Kawa, like virtually all Scheme environments, operates like a UNIX shell in that you type in commands, hit return, and expect something to happen in return. You launch the Kawa interpreter by typing /usr/class/cs107/bin/kawa at the command prompt. We’ve installed Kawa on the elaines, the myths, and the brambles, so you’ll need to work on one of those machines. jerry> /usr/class/cs107/bin/kawa #|kawa:1|# You interact with the Kawa environment by typing something in and then allowing Kawa to read what you typed, evaluate it, and then print a result. Sometimes, what you type is so trivial that the result is precisely what you typed in. jerry> /usr/class/cs107/bin/kawa #|kawa:1|# 4 4 #|kawa:2|# 3.14159 3.14159 #|kawa:3|# "Ben Newman has big hair" Ben Newman has big hair #|kawa:4|# #f #f #|kawa:5|# #t #t Okay, you’re not impressed yet, but there really is an intellectually compelling explanation for what just happened. You see, everything you type in has to evaluate to a single value. When the expressions we type in are constants, Kawa just echoes them right back at you. Constants (be they integer, floating point, string, or Boolean) evaluate to themselves. Typically, you get the Scheme environment to do some work for you. By typing out more elaborate expressions involving a function call, Scheme evaluates the call and eventually prints whatever the expression evaluated to. #|kawa:6|# (+ 1 2 3 4 5) 15 #|kawa:7|# (+ (* -40 (/ 9 5)) 32) -40
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
#|kawa:8|# (* 1 2 3 4 5 6 7 8 9 10) 3628800 #|kawa:9|# (/ 10 3) 10/3 #|kawa:10|# (quotient 10 3) 3 #|kawa:11|# (remainder 10 3) 1 #|kawa:12|# (positive? 45) #t #|kawa:13|# (negative? 0) #f #|kawa:14|# (zero? -45) #f #|kawa:15|# (sqrt 2) 1.4142135623730951 #|kawa:16|# (expt 7 (expt 5 3)) 433765494809799328253735475726318825169783299462040510174489301774456943272 0994168089672192211758909320807 Each of the nine examples above invokes some built-in function. Not surprisingly, Scheme provides every mathematical operator imaginable. Any reasonable language needs to. What is surprising is how we invoke a function in the first place. Function calls are expressed as lists, where function name and arguments are bundled in between open and close parentheses. All functions, including the binary ones like + and / , are invoked in prefix form. Each of the top-level items making up the function call gets evaluated, and once all sub-expressions have been evaluated, the top-level expression gets evaluated too. When you’ve had it and want to do something else, you leave the environment by
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/14/2010 for the course CS 107 taught by Professor Cain,g during the Spring '08 term at Stanford.

Page1 / 13

30-Introduction-To-Scheme - CS107 Spring 2007 Handout 30...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online