Date post: | 16-Jan-2016 |
Category: |
Documents |
Upload: | dale-skinner |
View: | 221 times |
Download: | 0 times |
Scheme
A Language of FunctionA Language of Function
JP Deshaies and Josh CallowayJP Deshaies and Josh Calloway
In the beginning there was LISP … and LISP begat Scheme.
Way back when FORTRAN was cool … The development of LISP was started in the late The development of LISP was started in the late
1950’s by John McCarthy and company (MIT) as 1950’s by John McCarthy and company (MIT) as and extension of the relatively new language and extension of the relatively new language FORTRAN.FORTRAN.
LISP was intended to simplify the processing of LISP was intended to simplify the processing of symbols and lists by using Polish type notation symbols and lists by using Polish type notation and functions. Scheme and LISP are type-less and functions. Scheme and LISP are type-less languages. languages.
LISP is the first “function”-al language.LISP is the first “function”-al language.
Yeah, but will it compile?
LISP went through approximately 3 revisions LISP went through approximately 3 revisions before an interpreter/compiler was even properly before an interpreter/compiler was even properly considered. Assembly code was hashed to considered. Assembly code was hashed to compile only certain components as needed.compile only certain components as needed.
The functional paradigm was credible and very The functional paradigm was credible and very useful and quite rapidly moved away from useful and quite rapidly moved away from FORTRAN.FORTRAN.
There were no standards devised this early and There were no standards devised this early and LISP began to change, evolve, and spread.LISP began to change, evolve, and spread.
I can make it speak with a LISP …
It became the favorite of AI researchers at MIT It became the favorite of AI researchers at MIT and elsewhere and continues to be the oldest and and elsewhere and continues to be the oldest and most widespread language in AI.most widespread language in AI.
Throughout the 60’s and 70’s, LISP’s lack of a Throughout the 60’s and 70’s, LISP’s lack of a real standard gave way to an incredible array of real standard gave way to an incredible array of dialects.dialects.
(Common LISP is probably the most widely used but I ran across (Common LISP is probably the most widely used but I ran across about 30 and references to many more.)about 30 and references to many more.)
I need rules, man!
In 1975, Sussman and Steele (MIT) decided to “modernize In 1975, Sussman and Steele (MIT) decided to “modernize and clean-up” LISP and create a practical version for and clean-up” LISP and create a practical version for scientific computation which they called Scheme.scientific computation which they called Scheme.
Scheme is and abbreviation of schemer, and was one of Scheme is and abbreviation of schemer, and was one of three AI languages derived about the same time. three AI languages derived about the same time.
((The other two, Planner and Conniver, didn’t really take off.) The other two, Planner and Conniver, didn’t really take off.)
Scheme was created to stress “conceptual elegance and Scheme was created to stress “conceptual elegance and simplicity” so often favored by scientists.simplicity” so often favored by scientists.
Interesting facts …
Scheme was one of the first programming languages Scheme was one of the first programming languages to incorporate first class procedures (procedures that to incorporate first class procedures (procedures that pass/return variables and functions).pass/return variables and functions).
The standard for Scheme is only about 50 pages The standard for Scheme is only about 50 pages whereas the standard for Common Lisp is about whereas the standard for Common Lisp is about 1300 pages.1300 pages.
Scheme supports multiple paradigms:Scheme supports multiple paradigms: FunctionalFunctional Object orientedObject oriented ImperativeImperative
More fun facts …
Its ability to present complex Its ability to present complex ideas/abstractions with simple primitives ideas/abstractions with simple primitives makes scheme a jack-of-all-trades teaching makes scheme a jack-of-all-trades teaching tool for entry level programmers.tool for entry level programmers.
Scheme (like LISP) has a multitude of Scheme (like LISP) has a multitude of decendants.decendants.
For example:For example:
Bigloo, Chez Scheme, Bigloo, Chez Scheme, Chicken, Elk, Gambit, Chicken, Elk, Gambit, Gauche, Guile, Gauche, Guile, Hotdog, Kawa, KSI, Hotdog, Kawa, KSI, KSM, Larceny, KSM, Larceny, LispMe, MIT Scheme, LispMe, MIT Scheme, OpenScheme, PLT OpenScheme, PLT Scheme, Pocket Scheme, Pocket Scheme, Scheme,
PS3I, QScheme, PS3I, QScheme, Rhizome/pi, RScheme, Rhizome/pi, RScheme, Scheme48, Scheme-Scheme48, Scheme-to-C, SCM, Silk, to-C, SCM, Silk, SISC, SIOD, Sizzle, SISC, SIOD, Sizzle, Stalin, STKlos, SXM, Stalin, STKlos, SXM, Systas, TinyScheme, Systas, TinyScheme, VSCM. VSCM.
Implementation Details
SchemeScheme
Comments
Comments start with a semi-colon and Comments start with a semi-colon and extend to the end of a given lineextend to the end of a given line
Example:Example: ; this is a comment ; this is a comment
What Scheme Does Have:
Lexical scopingLexical scoping uniform evaluation rulesuniform evaluation rules uniform treatment of data typesuniform treatment of data types
What Scheme Does Not Have
uninitialized variablesuninitialized variables
explicit storage managementexplicit storage management
Interacting With Scheme
Read-Eval-Print LoopRead-Eval-Print Loop ‘‘Read’ an expression from the Read’ an expression from the
keyboardkeyboard ‘‘Eval’uate the expressionEval’uate the expression ‘‘Print’ result to the screenPrint’ result to the screen
Data Types
Characters: #\a #\A #\space #\newlineCharacters: #\a #\A #\space #\newline Strings: “this is a string”Strings: “this is a string” Arrays: (called vectors) #(1 2 “str” #\x 5)Arrays: (called vectors) #(1 2 “str” #\x 5) Lists: (1 2 3)Lists: (1 2 3) Numbers: 47 1/3 2.3 4.5e10 1+3iNumbers: 47 1/3 2.3 4.5e10 1+3i
More Data Types
Functions: (also called procedures)Functions: (also called procedures) Booleans: #t (true) #f (or () ) (false)Booleans: #t (true) #f (or () ) (false) Ports: (e.g. open files)Ports: (e.g. open files) Symbols: a-symbol foo a55 c$23*47!Symbols: a-symbol foo a55 c$23*47! Atoms: 5 “some string” Atoms: 5 “some string”
foo ; can be numbers, foo ; can be numbers, strings, or symbolsstrings, or symbols
General Data Type Info
A vectors contents can be any A vectors contents can be any combination of data objects (types)combination of data objects (types)
Symbols can include the characters: Symbols can include the characters: + - . * / < = > ! ? : $ % _ & ~ and ^+ - . * / < = > ! ? : $ % _ & ~ and ^
Symbols are case insensitiveSymbols are case insensitive Symbols are used for identifiers Symbols are used for identifiers
(variable names)(variable names)
More General Info
Variables can hold values of any typeVariables can hold values of any type Names refer to values (not required)Names refer to values (not required) An expression is one of more forms An expression is one of more forms
between parentheses: ex. (+ 2 3 ) between parentheses: ex. (+ 2 3 )
Expressions
Constant: ‘foo #\z 3 “a string”Constant: ‘foo #\z 3 “a string” Variable reference: foo george +Variable reference: foo george + Function creation: (lambda (z) (* z z z))Function creation: (lambda (z) (* z z z)) Function application: (cube 27)Function application: (cube 27) Conditional: (if (< x 3) sqrt 16)Conditional: (if (< x 3) sqrt 16) Assignment: (set! x 5)Assignment: (set! x 5) Sequence: (begin (write x) (write y) )Sequence: (begin (write x) (write y) )
More Expressions
Predicates: (string? str) Predicates: (string? str) ; end with ? ; end with ?
Side-effecting Functions: (set! x 5) Side-effecting Functions: (set! x 5) ; end with !; end with !
Scheme versus C
Scheme operates with prefix notationScheme operates with prefix notation
C operates with infix notationC operates with infix notation
Scheme versus C
(+ 2 3 4)(+ 2 3 4) (2 + 3 + 4)(2 + 3 + 4) (< low x high) (< low x high) ((low<x) && (x<high))((low<x) && (x<high)) (+ (* 2 3) (* 4 5) ) (( 2 * 3 ) + ( 4 * 5 ))(+ (* 2 3) (* 4 5) ) (( 2 * 3 ) + ( 4 * 5 )) ( f x y )( f x y ) f ( x, y) f ( x, y)
Scheme --> ( define (sq x) (* x x) )Scheme --> ( define (sq x) (* x x) ) C --> int sq ( int x ) { return ( x * x ) }C --> int sq ( int x ) { return ( x * x ) }
Defining Functions Functions are defined with the Functions are defined with the
keyword ‘define’:keyword ‘define’: Example:Example:
(define (add-one x) (+ x 1))(define (add-one x) (+ x 1)) ; to call this function:; to call this function: (add-one (6))(add-one (6)) ; output; output 77
Predicates
Conditional expressions used to Conditional expressions used to direct the ‘flow’ of programsdirect the ‘flow’ of programs
Example:Example: (if (> 7 6 ) 1 2)(if (> 7 6 ) 1 2) ; if 7 > 6, expression evaluates ; if 7 > 6, expression evaluates
to 1, else the expression to 1, else the expression evaluates to 2evaluates to 2
More On Predicates
General predicate expressions:General predicate expressions: Eq? ; used for telling whether Eq? ; used for telling whether
two objects are the same (ie. a two objects are the same (ie. a list is only eq? to itself)list is only eq? to itself)
Equal? ; used to test if two lists Equal? ; used to test if two lists have the same elements in the have the same elements in the same ordersame order
User Defined Predicates
Example:Example:
(define (positive? x) (>= x 0))(define (positive? x) (>= x 0))
; then call positive; then call positive
(positive? 5)(positive? 5)
;evaluates to:;evaluates to:
#t#t
Lists
A sequence of itemsA sequence of items Main data structure of schemeMain data structure of scheme Example:Example:
( define listA ( list (1 2 3) ) )( define listA ( list (1 2 3) ) )
List Functions
car ; evaluates to the first element of a listcar ; evaluates to the first element of a list cdr ; removes the first element of a list and cdr ; removes the first element of a list and
evaluates to the remainder of the listevaluates to the remainder of the list list? ; this predicate returns #t (true) is its list? ; this predicate returns #t (true) is its
parameter is a listparameter is a list cons ; used to add elements to a listcons ; used to add elements to a list
Recursion
A Scheme program to A Scheme program to compute factorials iscompute factorials is
(define (fact x)(define (fact x)
if (= x 0)if (= x 0)
11
(* x (fact (- (* x (fact (- x 1))))x 1))))
Also recursive
Here is one to do Here is one to do powerspowers
X^NX^N
(define (power x n)(define (power x n)
(if (= n 0)(if (= n 0)
11
(* x (power x (- n 1)))))(* x (power x (- n 1)))))
Macros
““just as functions are semantic just as functions are semantic abstractions over operations, macros abstractions over operations, macros are textual abstractions over syntax”are textual abstractions over syntax”
these can be used to extend the base these can be used to extend the base languagelanguage
Macro Example
define ( macking, “something’ )define ( macking, “something’ ) ; then type the token ‘macking’ followed ; then type the token ‘macking’ followed
by the non-token cool”by the non-token cool” macking cool”macking cool” ; this, in turn, forms the string token:; this, in turn, forms the string token: ““something cool” something cool”
Scheme Implementations
Commercial Implementations:Commercial Implementations: Chez Scheme, and MacSchemeChez Scheme, and MacScheme
More Scheme Implementations PC Scheme, Scheme->C, Scheme86, PC Scheme, Scheme->C, Scheme86,
EdScheme, Scheme311, TekScheme, EdScheme, Scheme311, TekScheme, STING, Skim, Scm, T, Gambit, FDU STING, Skim, Scm, T, Gambit, FDU Scheme, SIOS, PaiLisp, PMLisp, MIT Scheme, SIOS, PaiLisp, PMLisp, MIT Scheme, UMB Scheme, Scheme48, Scheme, UMB Scheme, Scheme48, OakLisp, MultiScheme, Mul-T, OakLisp, MultiScheme, Mul-T, XScheme, Fool’s Lisp, ELK Scheme, XScheme, Fool’s Lisp, ELK Scheme, Vincenns Scheme, MultiLisp, ;+ (more)Vincenns Scheme, MultiLisp, ;+ (more)
Contemporary Use Of Lisp/Scheme
Companies that use Lisp / Scheme: Companies that use Lisp / Scheme: DEC, TI, Tektronix, HP, NASA, and SunDEC, TI, Tektronix, HP, NASA, and Sun
Other companies using Lisp / Scheme: Other companies using Lisp / Scheme: Content-Integrity, Memetrics, and RiderContent-Integrity, Memetrics, and Rider
Scheme Resources Rebecca’s Scheme Resource: Rebecca’s Scheme Resource: http://www.http://www.
cscs..uncaunca..eduedu/~/~brucebruce/Fall00/csci431/lecture8/lecture8.html/Fall00/csci431/lecture8/lecture8.html
MIT Scheme Site: MIT Scheme Site: http://www.http://www.swissswiss..aiai..mitmit..eduedu/projects/scheme//projects/scheme/
Schemers Organization Site:Schemers Organization Site: http://www.schemers.org/http://www.schemers.org/
The Internet Scheme Repository Home The Internet Scheme Repository Home PagePage http://www.http://www.cscs..indianaindiana..eduedu/scheme-repository/home.html/scheme-repository/home.html
More Scheme Resources The Scheme Underground Site The Scheme Underground Site
http://www.http://www.aiai..mitmit..eduedu/projects//projects/susu//susu.html.html Scheme Faq Scheme Faq
http://www-2.http://www-2.cscs..cmucmu..eduedu/Groups/AI/html//Groups/AI/html/faqsfaqs//langlang/scheme/top.html/scheme/top.html
Scheme Tutorial Scheme Tutorial http://www.http://www.cscs.rice..rice.eduedu/CS/PLT/Teaching/Lectures/Released/Bo/CS/PLT/Teaching/Lectures/Released/Book/ok/