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