DOC PREVIEW
UA CSC 520 - Scheme

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSc 520Principles of Programming Languages3: Scheme — IntroductionChristian CollbergDepartment of Computer ScienceUniversity of [email protected] 2005 Christian CollbergApril 22, 20051 Background• Scheme is based on LISP which was developed by John McCarthy in the mid 50s.• LISP stands for LISt Processing, not Lots of Irritating Silly Parentheses.• Functions and data share the same representation: S-Expressions.• A basic LISP implementation needs six functions cons, car, cdr, equal, atom, cond.• Scheme was developed by Sussman and Steele in 1975.2 S-Expressions• An S-Expression is a balanced list of parentheses.More formally, an S-expression is1. a literal (i.e., number, boolean, symbol, character, string, or empty list).2. a list of s-expressions.• Literals are sometimes called atoms.3 S-Expressions — ExamplesLegal Illegal66()(4 5)((5))(()())((4 5) (6 (7)))((5))()()(4 (5))(14 S-Expressions as Trees• An S-expression can be seen as a linear representation of tree-structure:2634263457(2 (3 4) (5 (6) 7))2(6)(3 4)5 S-Expressions as Function Calls• A special case of an S-expression is when the first element of a list is a function name.• Such an expression can be evaluated.> (+ 4 5)9> (add-five-to-my-argument 20)25> (draw-a-circle 20 45)#t6 S-Expressions as Functions• As we will see, function definitions are also S-expressions: ( d e f i n e ( f a ren h e i t − 2 − c e l s i u s f )( ∗ ( − f 32 ) 5 / 9 )) • So, Scheme really only has one syntactic structure, the S-expression, and that is used as a data-structure(to represent lists, trees, etc), as function definitions, and as function calls.7 Function Application• In general, a function application is written like this:(operator arg1arg2. . . argn)• The evaluation proceeds as follows:21. Evaluate operator. The result should be a function F.2. Evaluatearg1, arg2, . . . argnto getval1, val2, . . . valn3. Apply F to val1, val2, . . . valn.8 Function Application — Examples> (+ 4 5)9> (+ (+ 5 6) 3)14> 77> (4 5 6)eval: 4 is not a function> #t#t9 Atoms — NumbersScheme has• Fractions (5/9)• Integers (5435)• Complex numbers (5+2i)• Inexact reals (#i3.14159265)> (+ 5 4)9> (+ (* 5 4) 3)23> (+ 5/9 4/6)1.2> 5/90.5310 Atoms — Numbers. . .> (+ 5/9 8/18)1> 5+2i5+2i> (+ 5+2i 3-i)8+1i> (* 236542164521634 3746573426573425643)886222587860913289285513763860662> pi#i3.141592653589793> e#i2.718281828459045> (* 2 pi)#i6.28318530717958611 Atoms — Numbers. . .• Scheme tries to do arithmetic exactly, as much as possible.• Any computations that depend on an inexact value becomes inexact.• Scheme has many builtin mathematical functions:> (sqrt 16)4> (sqrt 2)#i1.4142135623730951> (sin 45)#i0.8509035245341184> (sin (/ pi 2))#i1.012 Atoms — Strings• A string is enclosed in double quotes.> (display "hello")hello> "hello""hello"> (string-length "hello")5> (string-append "hello" " " "world!")"hello world!"13 Atoms — Booleans• true is written #t.• false is written #f.4> #ttrue> #ffalse> (display #t)#t> (not #t)false14 Identifiers• Unlike languages like C and Java, Scheme allows identifiers to contain special characters, such as !$ % & * + - . / : < = > ? @ ^ ~ . Identifiers should not begin with a character that canbegin a number.• This is a consequence of Scheme’s simple syntax.• You couldn’t do this in Java because then there would be many ways to interpret the expression X-5+Y.Legal Illegalh-e-l-l-ogive-me!WTF?3some-stance15 Defining Variables• define binds an expression to a global name:(define name expression)(define PI 3.14)> PI3.14(define High-School-PI (/ 22 7))> High-School-PI3.14285716 Defining Functions• define binds an expression to a global name:(define (name arg1arg2...) expression)• arg1arg2... are formal function parameters.5(define (f) ’hello)> (f)hello(define (square x) (* x x))> (square 3)917 Defining Helper Functions• A Scheme program consists of a large number of functions.• A function typically is defined by calling other functions, so called helper or auxiliary functions.(define (square x) (* x x))(define (cube x) (* x (square x)))> (cube 3)2718 Preventing Evaluation• Sometimes you don’t want an expression to be evaluated.• For example, you may want to think of (+ 4 5) as a list of three elements +, 4, and 5, rather than asthe computed value 9.• (quote (+ 4 5)) prevents (+ 4 5) from being evaluated. You can also write ’(+ 4 5).> (display (+ 4 5))9> (display (quote (+ 4 5)))(+ 4 5)> (display ’(+ 4 5))(+ 4 5)19 Dr Scheme• Download DrScheme from here: http://www.drscheme.org.• It has already been installed for you in lectura and the Windows machines in the lab.• Start DrScheme under unix (on lectura) by saying> drscheme• On Windows and MacOS it may be enough to click on the DrScheme logo to start it up.620 Dr SchemeDefinitionswindowInteractionwindowLanguagelevelSelect languagelevelAddteachpacksSavedefinitions21 Dr Scheme — Using TeachPacks722 Dr Scheme — Using the Stepper23 References• Free interpreter: http://www.drscheme.org.• Manual: http://www.swiss.ai.mit.edu/projects/scheme/documentation/scheme.html• Tutorials:– 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/Scheme24 References. . .• Language reference manual: http://www.swiss.ai.mit.edu/ftpdir/scheme-reports/r5rs.ps.• Some of this material is taken from http://www.ecf.utoronto.ca/~gower/CSC326F/slides,cDianaInkpen 2002, Suzanne Stevenson 2001.25 Scheme so Far• A function is defined by(define (name arguments) expression)• A variable is defined by(define name expression)• Strings are inclosed in double quotes, like "this". Common operations on strings are8– (string-length string)– (string-append list-of-strings)• Numbers can be exact integers, inexact reals, fractions, and complex. Integers can get arbitrarily large.• Booleans are written #t and #f.26 Scheme so Far. . .• An inexact number is written: #i3.14159265.• Common operations on numbers are– (+ arg1 arg2), (- arg1 arg2)– (add1 arg), (sub1 arg)– (min arg1 arg2), (max arg1 arg2)• A function application is written:> (function-name arguments)• Quoting is used to prevent evaluation(quote


View Full Document

UA CSC 520 - Scheme

Documents in this Course
Handout

Handout

13 pages

Semantics

Semantics

15 pages

Haskell

Haskell

15 pages

Recursion

Recursion

18 pages

Semantics

Semantics

12 pages

Scheme

Scheme

32 pages

Syllabus

Syllabus

40 pages

Haskell

Haskell

17 pages

Scheme

Scheme

27 pages

TypeS

TypeS

13 pages

Scheme

Scheme

27 pages

Syllabus

Syllabus

10 pages

Types

Types

16 pages

FORTRAN

FORTRAN

10 pages

Load more
Download Scheme
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 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 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?