Unformatted text preview:

CS107 Handout 29Spring 2008 May 14, 2008Scheme BasicsMuch of this handout was leveraged off of some LISP handoutswritten by Todd Feldman and Nick Parlante.Getting StartedWe’ll be relying on an open-source application called Kawa to write and test our Schemecode. Kawa, like virtually all Scheme environments, operates like a UNIX shell in thatyou type in commands, hit return, and expect something to happen in return. Youlaunch the Kawa interpreter by typing /usr/class/cs107/bin/kawa at the commandprompt. We’ve installed Kawa on the elaines, the pods, and the myths, so you’ll need towork 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 allowingKawa to read what you typed, evaluate it, and then print a result. Sometimes, what youtype is so trivial that the result is precisely what you typed in.jerry> /usr/class/cs107/bin/kawa#|kawa:1|# 44#|kawa:2|# 3.141593.14159#|kawa:3|# "Ben Newman has big hair"Ben Newman has big hair#|kawa:4|# #f#f#|kawa:5|# #t#tOkay, you’re not impressed yet, but there really is an intellectually compellingexplanation for what just happened. You see, everything you type in has to evaluate toa single value. When the expressions we type in are constants, Kawa just echoes themright back at you. Constants (be they integer, floating point, string, or Boolean) evaluateto themselves.Typically, you get the Scheme environment to do some work for you. By typing outmore elaborate expressions involving a function call, Scheme evaluates the call andeventually prints whatever the expression evaluated to.#|kawa:6|# (+ 1 2 3 4 5)15#|kawa:7|# (+ (* -40 (/ 9 5)) 32)-40Please note that some of the resources referred to in this document require a Stanford Network Account and therefore may not be accessible.2#|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))4337654948097993282537354757263188251697832994620405101744893017744569432720994168089672192211758909320807Each of the nine examples above invokes some built-in function. Not surprisingly,Scheme provides every mathematical operator imaginable. Any reasonable languageneeds to.What is surprising is how we invoke a function in the first place. Function calls areexpressed as lists, where function name and arguments are bundled in between openand close parentheses. All functions, including the binary ones like + and /, are invokedin 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 getsevaluated too.When you’ve had it and want to do something else, you leave the environment bycalling exit.#|kawa:17|# (exit)jerry>3Scheme Data TypesScheme is actually a little more sophisticated than we’re going to make it out to be. Infact, we’re going to pretend—at least for a little while—that Scheme offers up only twotypes of data: primitives and lists. Primitives are the types that can’t be subdivided:integers, floating point numbers, rationals, characters, strings, and Booleans. Listscomprise the generic abstraction Scheme offers up for aggregating data. Scheme itselfallows for arrays (called vectors), and Kawa takes aggregation even further bysupporting records and classes. Initially, we’re going to ignore everything else andpretend that lists are all we have for bundling multiple pieces of information. It’s not atall an unreasonable thing to do, because Scheme programmers use the list more than allof the other aggregate types combined. (In fact, LISP is short for List Processing, andScheme and LISP are fundamentally the same language.)What primitives are there? Pretty much the ones you’d expect, plus a few others:Numbers: The set of all numeric constants includes:42, 2.818, 11/5, and 0.707-0.707i.Numbers can be either integral (42), real (2.818), rational (11/5), orcomplex (0.707-0.707i). You shouldn’t need to worry about whichsubtype you’re dealing with, since Scheme just manages the automaticconversions between them. Scheme provides the full spectrum ofmathematical functions: +, -, *, /, quotient, remainder, modulo, sqrt,expt, exp, sin, atan, and a good number of other ones. Asexpressions, numeric constants evaluate to themselves.Booleans: Scheme has a strong data type for the Boolean, just like C++ and Java.The Scheme equivalents of true and false are #t and #f, althougheverything other than #f is understood to be logically equivalent to #t.Of course, Boolean expressions evaluate to Boolean values and influencewhat code is executed when and how many times. Scheme provides thefull bag of logical operators for compound expressions: and, or, and not.As expressions, Boolean constants evaluate to themselves.Characters: Examples of character constants are:#\b, #\&, #\newline, and #\spaceNot surprisingly, these are the textual atoms that make up strings. Wedon’t have much reason to subdivide strings during our two-week crashcourse, so this is really just mentioned for completeness. Note that #\t4and #\f are character constants, but #t and #f are Boolean constants. AsScheme expressions, character constants evaluate to themselves.Strings: String constants are linear sequences of zero or more characters, and aredelimited by double quotes. As expressions, string constants evaluate tothemselves. Examples of string constants include:"David Hall likes Scheme","1234567890!@#$%^&*()","Nancy Pelosi's \"San Francisco Values\"", and"".Strings are bona fide data types with all forms of language support.Need to compare two strings? Use string=? (or string<?. Orstring>=? Or string-ci=? if you’re looking for case insensitivecomparisons.) Want to concatenate 10 "Not it! "’s together? Just usestring-append. Need to take the "fun" out of "dysfunctional"?Rely on substring.It’s not important to know all of the string-related functions just yet.Just understand that everything necessary to manipulate strings iseither provided or easily implemented in terms of what is.Symbols: Symbols are sequences of characters (without the double quotes) thatserve as general purpose identifiers. Examples include:i, power-set-of-rest, partition, is-compatible?,fraction->real,not, null?,


View Full Document

Stanford CS 107 - Scheme Basics

Download Scheme Basics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Scheme Basics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Scheme Basics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?