1 / 37

Scheme

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 ….

erwin
Download Presentation

Scheme

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scheme A Language of Function JP Deshaies and Josh Calloway

  2. In the beginning there was LISP … and LISP begat Scheme.

  3. 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.

  4. 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.

  5. 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.)

  6. 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.

  7. 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

  8. 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:

  9. 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.

  10. Implementation Details Scheme

  11. Comments • Comments start with a semi-colon and extend to the end of a given line • Example: • ; this is a comment

  12. What Scheme Does Have: • Lexical scoping • uniform evaluation rules • uniform treatment of data types

  13. What Scheme Does Not Have • uninitialized variables • explicit storage management

  14. Interacting With Scheme • Read-Eval-Print Loop • ‘Read’ an expression from the keyboard • ‘Eval’uate the expression • ‘Print’ result to the screen

  15. 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

  16. 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

  17. 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)

  18. 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 )

  19. 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) )

  20. More Expressions • Predicates: (string? str) ; end with ? • Side-effecting Functions: (set! x 5) ; end with !

  21. Scheme versus C • Scheme operates with prefix notation • C operates with infix notation

  22. 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 ) }

  23. 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

  24. 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

  25. 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

  26. User Defined Predicates • Example: (define (positive? x) (>= x 0)) ; then call positive (positive? 5) ;evaluates to: #t

  27. Lists • A sequence of items • Main data structure of scheme • Example: ( define listA ( list (1 2 3) ) )

  28. 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

  29. A Scheme program to compute factorials is (define (fact x) if (= x 0) 1 (* x (fact (- x 1)))) Recursion

  30. Here is one to do powers X^N (define (power x n) (if (= n 0) 1 (* x (power x (- n 1))))) Also recursive

  31. Macros • “just as functions are semantic abstractions over operations, macros are textual abstractions over syntax” • these can be used to extend the base language

  32. 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”

  33. Scheme Implementations • Commercial Implementations: • Chez Scheme, and MacScheme

  34. 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)

  35. 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

  36. 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

  37. 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/

More Related