Csc 372 Comparative Programming Languages Background S-Expressions S-Expressions
Total Page:16
File Type:pdf, Size:1020Kb
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 c 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) #t (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 MacLisp 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 compiler, 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 Interlisp 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 Common Lisp 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 microcode 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 Lisp Machine Lisp 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 Symbolics 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) workstation. 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.