Background

Scheme is based in LISP which was developed by John CSc 372 McCarthy in the mid 50s. LISP stands for LISt Processing, not Lots of Irritating Comparative Programming Silly Parentheses. Languages Functions and data share the same representation: 3: Scheme — Introduction S-Expressions. A basic LISP implementation needs six functions Christian Collberg cons, car, cdr, equal, atom, cond. [email protected] Scheme was developed by Sussman and Steele in Department of Computer Science 1975. University of Arizona

Copyright 2004 Christian Collberg

372—Fall 2004—3 [1] 372—Fall 2004—3 [2]

S-Expressions S-Expressions — Examples

An S-Expression is a balanced list of parentheses. Legal Illegal 66 More formally, an S-expression is ( () 1. a literal (i.e., number, boolean, symbol, character, (5)) (4 5) string, or empty list) (these are sometimes called ()() ((5)) atoms). (4 (5) (()()) 2. a list of s-expressions. )( ((4 5) (6 (7)))

372—Fall 2004—3 [3] 372—Fall 2004—3 [4] S-Expressions as Trees S-Expressions as Function Calls

An S-expression can be seen as a linear representation A special case of an S-expression is when the first of tree-structure: element of a list is a function name. Such an expression can be evaluated. 2 2 (6) (3 4) > (+ 4 5) 9 > (add-five-to-my-argument 20) 6 3 4 25 > (draw-a-circle 20 45) # (2 (3 4) (5 (6) 7))

2

3 4 5 7

6 372—Fall 2004—3 [5] 372—Fall 2004—3 [6]

S-Expressions as Functions Function Application. . .

As we will see, function definitions are also In general, a function application is written like this: S-expressions: (operator arg1 arg2 . . . argn) § ¤ ( define ( farenheit−2−celsius f ) The evaluation proceeds as follows: ( ∗ ( − f 3 2 ) 5 / 9 ) 1. Evaluate operator. The result should be a function ) F. ¦ ¥ 2. Evaluate So, Scheme really only has one syntactic structure, the arg1, arg2, . . . argn S-expression, and that is used as a data-structure (to to get represent lists, trees, etc), as function definitions, and val1, val2, . . . valn as function calls. 3. Apply F to val1, val2, . . . valn.

372—Fall 2004—3 [7] 372—Fall 2004—3 [8] Function Application — Examples Numbers

> (+ 4 5) Scheme has 9 Fractions (5/9) > (+ (+ 5 6) 3) 14 Integers (5435) > 7 Complex numbers (5+2i) 7 Inexact reals (#i3.14159265) > (4 5 6) eval: 4 is not a function > (+ 5 4) > #t 9 #t > (+ (* 5 4) 3) 23 > (+ 5/9 4/6) 1.2 > 5/9 0.5

372—Fall 2004—3 [9] 372—Fall 2004—3 [10]

Numbers. . . Numbers. . .

> (+ 5/9 8/18) Scheme tries to do arithmetic exactly, as much as 1 possible. > 5+2i Any computations that depend on an inexact value 5+2i becomes inexact. > (+ 5+2i 3-i) 8+1i Scheme has many builtin mathematical functions: > (* 236542164521634 3746573426573425643) 886222587860913289285513763860662 > (sqrt 16) > pi 4 #i3.141592653589793 > (sqrt 2) > e #i1.4142135623730951 #i2.718281828459045 > (sin 45) > (* 2 pi) #i0.8509035245341184 #i6.283185307179586 > (sin (/ pi 2)) #i1.0

372—Fall 2004—3 [11] 372—Fall 2004—3 [12] Identifiers Defining Variables

Unlike languages like C and Java, Scheme allows define binds an expression to a global name: identifiers to contain special characters, such as (define name expression) ! $ % & * + - . / : < = > ? @ ˆ ˜ . Identifiers should not begin with a character that can > (define PI 3.14) begin a number. This is a consequence of Scheme’s simple syntax. > PI You couldn’t do this in Java because then there would 3.14 be many ways to interpret the expression X-5+Y. > (define High-School-PI (/ 22 7)) Legal Illegal > High-School-PI h-e-l-l-o 3some 3.142857 give-me! -stance WTF?

372—Fall 2004—3 [13] 372—Fall 2004—3 [14]

Defining Functions Defining Helper Functions

define binds an expression to a global name: A Scheme program consists of a large number of functions. (define (name arg1 arg2 ...) expression) One function typically is defined by calling other arg1 arg2 ... are formal function parameters. functions, so called helper or auxiliary functions.

> (define (f) ’hello) > (define (square x) (* x x))

> (f) > (define (cube x) (* x (square x))) hello > (cube 3) > (define (square x) (* x x)) 27 > (square 3) 9

372—Fall 2004—3 [15] 372—Fall 2004—3 [16] References References. . .

Free interpreter: http://www.drscheme.org. Language reference manual: Manual: http://www.swiss.ai.mit.edu/ftpdir/scheme-reports/r5rs.ps. http://www.swiss.ai.mit.edu/projects/scheme/documentation/scheme.html Some of this material is taken from Tutorials: http://www.ecf.utoronto.ca/˜gower/CSC326F/slides, c Diana Inkpen 2002, Suzanne Stevenson 2001. http://ai.uwaterloo.ca/˜dale/cs486/s99/scheme-tutorial.html

http://cs.wwc.edu/%7Ecs_dept/KU/PR/Scheme.html

http://www.cis.upenn.edu/%7Eungar/CIS520/scheme-tutorial.html

http://dmoz.org/Computers/Programming/Languages/Lisp/Scheme

372—Fall 2004—3 [17] 372—Fall 2004—3 [18]

History of the Lisp Language History of the Lisp Language. . .

http://www.apl.jhu.edu/Ähall/text/Papers/Brief-History-of-Lisp.ps improved on the Lisp 1.5 notion of special variables and error handling. The following information is derived from the history section of dpANS Common MacLisp also introduced theconcept of functions that could take a variable number Lisp. of arguments, macros, arrays, non-local dynamic exits, fast arithmetic, the first good Lisp , and an emphasis on execution speed. Lisp is a family of languages with a long history. Early key ideas in Lisp were developed by John McCarthy during the 1956 Dartmouth Summer Research introduced many ideas into Lisp programming environments and Project on Artificial Intelligence. McCarthy’s motivation was to develop analgebraic methodology. One of the Interlisp ideasthat influenced was an list processing language for artificial intelligence work. Implementation efforts for iteration construct implemented by Warren Teitelman that inspired the loop macro early dialects of Lisp were undertaken on the IBM 704, the IBM 7090, the Digital used both on the Lisp Machines and in MacLisp, and now in Common Lisp. Equipment Corporation (DEC) PDP-1, the DECPDP-6, and the PDP-10. The primary dialect of Lisp between 1960 and 1965 was Lisp 1.5. By the early 1970’s there were two predominant dialects of Lisp, both arising from these early efforts: MacLisp and Interlisp. Forfurther information about very early Lisp dialects, see The Anatomy of Lisp or Lisp 1.5 Programmer’s Manual.

372—Fall 2004—3 [19] 372—Fall 2004—3 [20] History of the Lisp Language. . . History of the Lisp Language. . .

Although the first implementations of Lisp were on the IBM 704 and the IBM 7090, The Lisp machine concept was developed in the late 1960’s. In the early 1970’s, later work focussed on theDEC PDP-6 and, later, PDP-10 computers, the latter Peter Deutsch, working withDaniel Bobrow, implemented a Lisp on the Alto, a being the mainstay of Lisp and artificial intelligence work at such places as single-user minicomputer, using to interpret a byte-code Massachusetts Institute of Technology (MIT), Stanford University, and Carnegie implementation language. Shortly thereafter, Richard Greenblatt began work on a Mellon University(CMU) from the mid-1960’s through much of the 1970’s. The different hardwareand instruction set design at MIT. Although the Alto was not a PDP-10 computer and its predecessor the PDP-6 computer were, by design, total success as a Lisp machine, a dialect of Interlisp known as Interlisp-D became especially well-suited to Lisp because they had 36-bit words and 18-bit addresses. available on the D-series machines manufactured by Xerox—the Thisarchitecture allowed a cons cell to be stored in one word; single instructions Dorado,Dandelion, Dandetiger, and Dove (or Daybreak). An upward-compatible could extract the car and cdr parts. The PDP-6 and PDP-10 had fast, powerful extension of MacLisp called became available on the early MIT stack instructions that enabled fast function calling. But the limitations ofthe Lisp Machines. Commercial Lisp machines from Xerox, LispMachines (LMI), and PDP-10 were evident by 1973: it supported a small number of researchers using were on the market by 1981. During the late 1970’s, Lisp Machine Lisp Lisp, and the small, 18-bit address space (262,144 36-bit words) limited the size of began to expand towards a much fuller language. Sophisticated lambdalists, setf, a single program. One response to the address spaceproblem was the Lisp multiple values, and structures like those in Common Lisp are the results of early Machine, a special-purpose computer designed to run Lisp programs. The other experimentation with programming styles by the Lisp Machine group. response was to use general-purpose computers with address spaces larger than 18 bits, such as the DEC VAX and the S-1 MarkIIA.

372—Fall 2004—3 [21] 372—Fall 2004—3 [22]

History of the Lisp Language. . . History of the Lisp Language. . .

Jonl White and others migrated these features to MacLisp. Around 1980, Scott Richard P. Gabriel began the design of a Lisp to run on the S-1 Mark IIA Fahlman and others at CMU began work on a Lisp to run on the Scientific supercomputer. S-1 Lisp, nevercompletely functional, was the test bed for Personal Integrated Computing Environment (SPICE) . One of the adapting advanced compiler techniques to Lisp implementation. Eventually the goals of the project was to design a simpler dialect thanLisp Machine Lisp. S-1 and NIL groups collaborated. The Macsyma group at MIT began a project during the late 1970’s called the New The first effort towards Lisp standardization was made in 1969, when Anthony Implementation of Lisp (NIL)for the VAX, which was headed by White. One of the Hearn and Martin Griss at theUniversity of Utah defined Standard Lisp—a subset stated goals of the NIL project was to fix many of the historic, but annoying, of Lisp 1.5 and other dialects—to transport REDUCE, a symbolic algebra system. problems with Lisp while retaining significant compatibility with MacLisp. During the 1970’s, the Utah group implemented first a retargetable optimizing compilerfor Standard Lisp, and then an extended implementation known as (PSL). By the mid 1980’s, PSL ran on about a dozen kinds of computers.

372—Fall 2004—3 [23] 372—Fall 2004—3 [24] History of the Lisp Language. . . History of the Lisp Language. . .

PSL and —a MacLisp-like dialect for Unix machines—were the first In the late 1970’s object-oriented programming concepts started to make a strong examples of widely availableLisp dialects on multiple hardware platforms. One of impact . At MIT, certainideas from Smalltalk made their way into several the most important developments in Lisp occurred during the second half of the widely used programming systems. Flavors, an object-oriented programming 1970’s: Scheme. Scheme,designed by Gerald J. Sussman and Guy L. Steele Jr., system with multiple inheritance, was developed at MIT for the Lisp machine is a simple dialect of Lisp whose design brought to Lisp some of the ideas from community byHoward Cannon and others. At Xerox, the experience with Smalltalk semantics developed in the 1960’s. Sussman was one of and Knowledge Representation Language (KRL) led to the development of Lisp the primeinnovators behind many other advances in Lisp technology from the late Object Oriented Programming System (LOOPS) and later Common LOOPS. 1960’s through the 1970’s. The major contributions of Scheme were lexical These systems influenced the design of the Common Lisp Object System (CLOS). scoping, lexical closures, first-class continuations, and simplified syntax CLOS was developedspecifically for ’s standardization effort, and was (noseparation of value cells and function cells). Some of these contributions made separately written up in “Common Lisp Object System Specification.” However, a large impact on the design of Common Lisp. For further information about minor details of its design have changed slightly since that publication, and that Scheme, see IEEE Standard for the Scheme Programming Languageor “Revised4 papershould not be taken as an authoritative reference to the semantics of the Report on the Algorithmic Language Scheme.” Common Lisp Object System.

372—Fall 2004—3 [25] 372—Fall 2004—3 [26]

History of the Lisp Language. . . History of the Lisp Language. . .

In 1980 Symbolics and LMI were developing Lisp Machine Lisp; stock-hardware In 1986 X3J13 was formed as a technical working group to produce a draft for an implementation groups weredeveloping NIL, Franz Lisp, and PSL; Xerox was ANSI Common Lisp standard.Because of the acceptance of Common Lisp, the developing Interlisp; and the SPICE project at CMU was developing a MacLisp-like goals of this group differed from those of the original designers. These new goals dialect of Lisp called SpiceLisp. In April 1981, after a DARPA-sponsored meeting included stricter standardization for portability, an object-oriented programming concerning the splintered Lisp community, Symbolics, theSPICE project, the NIL system, acondition system, iteration facilities, and a way to handle large character project, and the S-1 Lisp project joined together to define Common Lisp. Initially sets. To accommodate those goals, a new language specification was developed. spearheaded by White and Gabriel, the driving force behind this grassroots effort was provided by Fahlman, DanielWeinreb, David Moon, Steele, and Gabriel. Common Lisp was designed as a description of a family of languages. The primary influences on Common Lisp were Lisp Machine Lisp, MacLisp, NIL, S-1 Lisp, , andScheme. Common Lisp: The Language is a description of that design. Its semantics were intentionally underspecified in places where it was felt that a tight specification would overly constrain Common Lisp researchand use.

372—Fall 2004—3 [27] 372—Fall 2004—3 [28] John McCarthy Guy Steele

Guy L. Steele Jr. is a Distinguished Engineer at Sun Microsystems, Inc. He received his A.B. in applied mathematics from Harvard College (1975), and his John McCarthy has been Professor of Computer Science at S.M. and Ph.D. in computer science and artificial intelligence from M.I.T. (1977 Stanford University since 1962. His research is mainly in and 1980). He has also been an assistant professor of computer science at artificial intelligence. Long ago he originated the Lisp Carnegie-Mellon University; a member of technical staff at Tartan Laboratories in programming language and the initial research on general Pittsburgh, Pennsylvania; and a senior scientist at Thinking Machines Corporation. purpose time-sharing computer systems. He joined Sun Microsystems in 1994. http://www-formal.stanford.edu/jmc/personal.html

372—Fall 2004—3 [29] 372—Fall 2004—3 [30]

Guy Steele. . .

The Association for Computing Machinery awarded him the 1988 Grace Murray Hopper Award and named him an ACM Fellow in 1994. He has served on accredited standards committees X3J11 (C language) and X3J3 (Fortran) and is currently chairman of X3J13 (Common Lisp). He was also a member of the IEEE committee that produced the IEEE Standard for the Scheme Programming Language, IEEE Std 1178-1990. He has had chess problems published in Chess Life and Review and is a Life Member of the United States Chess Federation. He has sung in the bass section Gerald Jay Sussman is the Matsushita Professor of Electrical Engineering at the of the MIT Choral Society and the Masterworks Chorale as well as in choruses Massachusetts Institute of Technology. He received the S.B. and the Ph.D. with the Pittsburgh Symphony Orchestra at Great Woods and with the Boston degrees in mathematics from the Massachusetts Institute of Technology in 1968 Concert Opera. He has played the role of Lun Tha in The King and I and the title and 1973, respectively. He has been involved in artificial intelligence research at role in Li’l Abner. He designed the original command set and was the first M.I.T. since 1964. His research has centered on understanding the person to port TeX. problem-solving strategies used by scientists and engineers, with the goals of

http://www.sls.csail.mit.edu/Ähurley/guysteele.html automating parts of the process and formalizing it to provide more effective methods of science and engineering education. Sussman has also worked in http://encyclopedia.thefreedictionary.com/Guy%20Steele computer languages, in computer architecture and in VLSI design. 372—Fall 2004—3 [31] 372—Fall 2004—3 [32] Gerald Jay Sussman. . . Beating the Averages

Sussman is a coauthor (with and Julie Sussman) of the introductory computer science textbook used at M.I.T. The textbook, "Structure and (This article is based on a talk given at the Franz Developer Interpretation of Computer Programs," has been translated into French, German, Symposium in Cambridge, MA, on March 25, 2001.) Chinese, Polish, and Japanese. As a result of this and other contributions to http://paulgraham.com/avg.html computer-science education, Sussman received the ACM’s Karl Karlstrom In the summer of 1995, my friend Robert Morris and I Outstanding Educator Award in 1990, and the Amar G. Bose award for teaching in started a startup called Viaweb. Our plan was to write 1991. that would let end users build online stores. What Sussman’s contributions to Artificial Intelligence include problem solving by was novel about this software, at the time, was that it ran on debugging almost-right plans, propagation of constraints applied to electrical our server, using ordinary Web pages as the interface. [· · ·] circuit analysis and synthesis, dependency-based explanation and Another unusual thing about this software was that it was dependency-based backtracking, and various language structures for expressing written primarily in a programming language called Lisp. problem-solving strategies. Sussman and his former student, Guy L. Steele Jr., invented the Scheme programming language in 1975.

http://www.swiss.ai.mit.edu/Ägjs/gjs.html

372—Fall 2004—3 [33] 372—Fall 2004—3 [34]

Beating the Averages Beating the Averages

It was one of the first big end-user applications to be written So you could say that using Lisp was an experiment. Our in Lisp, which up till then had been used mostly in hypothesis was that if we wrote our software in Lisp, we’d universities and research labs. Lisp gave us a great be able to get features done faster than our competitors, advantage over competitors using less powerful languages. and also to do things in our software that they couldn’t do. A company that gets software written faster and better will, And because Lisp was so high-level, we wouldn’t need a all other things being equal, put its competitors out of big development team, so our costs would be lower. [· · ·] business. And when you’re starting a startup, you feel this What were the results of this experiment? Somewhat very keenly. Startups tend to be an all or nothing surprisingly, it worked. We eventually had many proposition. You either get rich, or you get nothing. [· · ·] competitors, on the order of twenty to thirty of them, but Robert and I both knew Lisp well, and we couldn’t see any none of their software could compete with ours. We had a reason not to trust our instincts and go with Lisp. We knew wysiwyg online store builder that ran on the server and yet that everyone else was writing their software in C++ or Perl. felt like a desktop application. Our competitors had cgi But we also knew that that didn’t mean anything. If you scripts. And we were always far ahead of them in features. chose technology that way, you’d be running Windows. [· · ·] Sometimes, in desperation, competitors would try to introduce features that we didn’t have.

372—Fall 2004—3 [35] 372—Fall 2004—3 [36] Beating the Averages Beating the Averages

But with Lisp our development cycle was so fast that we Few would dispute, at least, that high level languages are could sometimes duplicate a new feature within a day or more powerful than machine language. Most programmers two of a competitor announcing it in a press release. By the today would agree that you do not, ordinarily, want to time journalists covering the press release got round to program in machine language. Instead, you should program calling us, we would have the new feature too. in a high-level language, and have a compiler translate it [· · ·]by word of mouth mostly, we got more and more users. into machine language for you. This idea is even built into By the end of 1996 we had about 70 stores online. At the the hardware now: since the 1980s, instruction sets have end of 1997 we had 500. Six months later, when Yahoo been designed for rather than human bought us, we had 1070 users. Today, as Yahoo Store, this programmers. [· · ·] software continues to dominate its market. [· · ·] During the years we worked on Viaweb I read a lot of job I’ll begin with a shockingly controversial statement: descriptions. A new competitor seemed to emerge out of programming languages vary in power. the woodwork every month or so. The first thing I would do, after checking to see if they had a live online demo, was look at their job listings. After a couple years of this I could tell which companies to worry about and which not to.

372—Fall 2004—3 [37] 372—Fall 2004—3 [38]

Beating the Averages

[· · ·] The safest kind were the ones that wanted Oracle experience. You never had to worry about those. You were also safe if they said they wanted C++ or Java developers. If they wanted Perl or Python programmers, that would be a bit frightening– that’s starting to sound like a company where the technical side, at least, is run by real hackers. If I had ever seen a job posting looking for Lisp hackers, I would have been really worried. [· · ·] Exercise sed ’s/Lisp/Scheme/g’

372—Fall 2004—3 [39]