Docsity

Scheme Programming: Algorithmic Language and Procedures, Study notes of Computer Science

Toggle sidebar
bg1
Com S 342
The Algorithmic Language Scheme
Overview
DrScheme, Chez Scheme, SLIB, and EOPL2-extensions
Features of Scheme
Expressions
Data types
Procedures
References
Revised5Report on the Algorithmic Language Scheme.
Daniel P. Friedman and Matthias Felleisen, “The Little Schemer”, Fourth Edition,
MIT Press, 1996
Harold Abelson et al., “Structure and Interpretation of Computer Programs”, MIT
Press, 1996
Daniel P. Friedman et al., “Essentials of Programming Languages”, Second Edition,
MIT Press, 2001
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download Scheme Programming: Algorithmic Language and Procedures and more Study notes Computer Science in PDF only on Docsity!

Com S 342

Chez Scheme, SLIB, and EOPL2-extensions „ DrScheme (orChez Scheme) is a complete high-performanceimplementation of ANSI/IEEE standard Scheme. „ SLIB is a portable Scheme library meant to provide compatibilityand utility functions for all standard Scheme implementations. „ Running Chez Scheme (Com S342): „ SCHEME_LIBRARY_PATH: The SLIB library directory. „ CS342LIB: The EOPL2-extensions path „ On every platform use the command “scheme” to start an interactiveScheme-session. „ The file “chez.prelude” defines everything you need to use Chez Scheme inCom S342. This file is loaded after “chez.init” – the SLIB initialization file.

Com S 342

CS342 Chez Scheme

Com S 342

Scheme „ Scheme is a statically scoped programming language. „ Each use of a variable is associated with a lexically apparent binding of thatvariable. „ Scheme is a dynamically typed language. „ Types are associated with values rather than with variables. „ Scheme has latent as opposed to manifest types. „ All objects in the course of a Scheme computation have unlimited extent. „ No Scheme object is ever destroyed. „ However, if an object becomes inaccessible, then it can be garbage-collected. „ Procedures in Scheme are first-class. „ Procedures can be created dynamically, stored in data structures, returned asresults of procedures, etc.

Com S 342

Identifiers „ Most identifies allowed by other programming languages are alsoacceptable to Scheme. „ Identifiers begin with a character or a special character, followedby a sequence of characters, special characters, or digits. „ Lexical structure: ::= * | ::= | ::= ‘!’ | ‘$’ | ‘%’ | ‘&’ | ‘*’ | ‘/’ | ‘:’ | ‘<’ | ‘=’ | ‘>’ | ‘?’ | ‘^’ | ‘_’ | ‘~’ ::= | | ::= ‘+’ | ‘-’ | ‘.’ | ‘@’ ::= ‘+’ | ‘-’ | ‘…’

Com S 342

Examples of Identifierslambda^

q list->vector^

soup +^

V17a <=?^

a34kTMNs the-word-recursion-has-many-meanings „ Any identifier may be used as a variable or as a syntactic keyword. „ When an identifier appears as a literal or within a literal, it is being used todenote a symbol, that is, equality (eqv?) is defined for identifiers.

Com S 342

Type PredicatesNo object satisfies more than one of the following predicates:boolean?

pair? symbol?^

number? char?^

string? vector?^

port? procedure? These predicates define the types

boolean*,pair,

symbol,number,

character,

string,vector,

port, andprocedure.

  • All values in Scheme count as true except ‘#f’.Note, the empty list is a special object of its own type; it satisfies none of thetype predicates.
  • Com S
  • The Algorithmic Language SchemeOverview „ DrScheme, Chez Scheme, SLIB, and EOPL2-extensions „ Features of Scheme „ Expressions „ Data types „ ProceduresReferences^5 „ Revised Report on the Algorithmic Language Scheme. „ Daniel P. Friedman and Matthias Felleisen, “The Little Schemer”, Fourth Edition,MIT Press, 1996 „ Harold Abelson et al., “Structure and Interpretation of Computer Programs”, MITPress, 1996 „ Daniel P. Friedman et al., “Essentials of Programming Languages”, Second Edition,MIT Press,
    • Com S
  • Variable References „ An expression consisting of a variable is a variable reference. The value of thevariable reference is the value stored in the location to which the variable isbound.Syntax:Example:> (define x 28)> x

Com S 342

Procedure Calls „ A procedure call is written by simply enclosing in parenthesis expressions forthe procedure to be called and the arguments to be passed to it.Syntax:^ ( {}

*^ )

Examples:^

> (+ 3 4) 7 > ((if #f + *) 3 4) 12 „^ Scheme uses the call-by-value parameter passing mechanism; everyargument is evaluated, including the expression that denotes the procedure,before the procedure is called.

Com S 342

Load a Script and Exit „ To load a script, the Scheme system provides the built-in procedure “load”.Syntax: (load )Example:^ > (load “test.ss”)> (load “fib.scm”) „ To leave the Scheme interpreter, call “exit”.Syntax: (exit)

Com S 342

Definitions „ Definitions are used to assign expressions to names.Syntax:^

(define )

Examples: > (define add

(lambda (x) (+ x 3)))> (add3 4) 7 > (define first car)> (first `(1 2)) 1

Com S 342

Assignment „ The assignment expression is used to store the value of an expression in alocation, which is bound to a variable.Syntax:^

(set! ) Example:^

> (define x 2)> (+ x 1) 3 > (set! x 4)> (+ x 1) 5 Note: must be bound either in some enclosing region (as in theexample) or at top level (in the program).

Com S 342

Other Expressions „ Sequencing:^

(begin {}+ ) „^ Iteration:

(do ( {( )}

+^ )

( {}

*^ )

{}

+^ )

„^ Binding:^

(let ({}

+^ ) ) ::= ( )

Com S 342

A Small Example – Bubble Sort(define bubble-sort^

; procedure bubble-sort (lambda (lon)

; procedure with one argument (if (null? lon)

; test whether list is empty `()^

; if list is empty, we are done ; set variable lon-with-elem-left(let ((lon-with-max-elem-left (reverse (put-max-last lon)))); build sorted list using recursion(cons (car lon-with-max-elem-left) ; select head of lon-with-max-elem-left; apply bubble-sort to the tail of lon-with-max-elem-left(bubble-sort (cdr lon-with-max-elem-left))))) ; end if) ; end lambda) ; end define