Scheme : variant of LISP

Slides:



Advertisements
Similar presentations
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Advertisements

1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
TES3111 October 2001 Artificial Intelligence LISP.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
JavaScript, Third Edition
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
Functional Programming in Scheme
ISBN Chapter 15 Functional Programming Languages.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Ada, Scheme, R Emory Wingard. Ada History Department of Defense in search of high level language around Requirements drafted for the language.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Functional Programming
Abstract Syntax cs7100 (Prasad) L7AST.
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming
History of Computing – Lisp
CS 3304 Comparative Languages
Section 15.4, 15.6 plus other materials
A Simple Syntax-Directed Translator
CS 326 Programming Languages, Concepts and Implementation
Introduction to Scheme
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
CS 326 Programming Languages, Concepts and Implementation
Lists in Lisp and Scheme
Closures and Streams cs784(Prasad) L11Clos
CSC 533: Organization of Programming Languages Spring 2008
Original material by Eric Grimson
Java Programming: From Problem Analysis to Program Design, 4e
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
The Metacircular Evaluator
CS 36 – Chapter 11 Functional programming Features Practice
Lisp and Scheme I.
Abstract Syntax cs7100 (Prasad) L7AST.
6.001 SICP Variations on a Scheme
6.001 SICP Interpretation Parts of an interpreter
Modern Programming Languages Lecture 18 Fakhar Lodhi
topics interpreters meta-linguistic abstraction eval and apply
CSC 533: Organization of Programming Languages Spring 2007
To understand recursion, one must first understand recursion.
Lisp.
Presentation transcript:

Scheme : variant of LISP LISP : John McCarthy (1958) Scheme : Steele and Sussman (1973) Those who do not learn from history are doomed to repeat it. - George Santayana C/Pascal : Procedural / Imperative Java/C++: Object-oriented style Scheme/LISP : Functional style General purpose languages vs scripting languages cs3180(Prasad) L12Scm

Scheme (now called Racket) Scheme = LISP + ALGOL symbolic list manipulation block structure; static scoping Symbolic Computation Translation Parsers, Compilers, Interpreters. Querying / Reasoning Natural language understanding systems Database querying Searching / Question-Answering Reasoners: make explicit information implicit in the facts and rules First-order logic deduction, common-sense reasoning cs3180(Prasad) L12Scm

“Striking” Features Simple and uniform syntax Support for convenient list processing (Automatic) Garbage Collection Environment for Rapid Prototyping Intrinsically generic functions Dynamic typing (flexible but inefficient) Compositionality through extensive use of lists. (minimizing impedance mismatch) Trivial syntax, minimal punctuations --- useful for meta-programming Code a solution before getting an optimal one : rapid prototyping no extra syntax for generics : other languages overspecific reliability not sacrificed. cf. static typing, OOPL’s polymorphic type system cs3180(Prasad) L12Scm

Expressions Literals Variables Procedure calls Literals Variables numerals(2), strings( “abc” ), boolean( #t ), etc. Variables Identifier represents a variable. Variable reference denotes the value of its binding. x Cf. Java reference types ref 5 cs3180(Prasad) L12Scm

Scheme Identifiers E.g., y, x5, +, two+two, zero?, list->string, etc (Illegal) 5x, y)2, ab c, etc Identifiers reserved keywords variables pre-defined functions/constants ordinary functions = procedures Not case sensitive In Java : two+two counts as 3 tokens (two, +, two). Blanks mandatory. ABC= AbC = abc = abC = … In Scheme: two+two is just one token. cs3180(Prasad) L12Scm

Procedure Call (application) (operator-expr operand-expr ...) prefix expression (proc/op arg1 arg2 arg3 ...) (+ x (p 2 3)) Function value ( (f 2 3) 5 6) f is Higher-order function The operator-expression must yield a function value compatible with the operand expressions. (f 2 3) must return a binary function on integers. Higher-order functions. Order of evaluation of the sub-expressions is “explicitly” left unspecified by Scheme. cf. C is silent about it. cf. Java specifies a left to right processing. cs3180(Prasad) L12Scm

Simple Scheme Expressions (+  12  13)    = 25 (/  12  14)    = 6/7 (+ 2.2+1.1i 2.2+1.1i ) = 4.4+2.2i (* 2.2+1.1i   0+i ) = (-1.1+2.2i) (< 2.2 3 4.4 5) = #t (positive? 25) = #t (negative? (- 1 0)) = #f (expt 2 10) = 1024 (sqrt 144) = 12 (string->number “12” 8) = 10 (string->number “AB” 16) = 171 (string->number “ABC”) = #f 10*16 + 11 = 171 cs3180(Prasad) L12Scm

Scheme Evaluation Rule (REPL) Expression blank separated, parentheses delimited, nested list structure Evaluation Evaluate each element of the outerlist recursively. Apply the result of the operator expression (of function type) to the results of zero or more operand expressions. The syntax of a program is well-matched with the basic data structure the programs manipulate to facilitate META-PROGRAMMING. “program-manipulating programs” REPL: Read Eval Print Loop cs3180(Prasad) L12Scm

(+ (* 2 3) (- 4 (/ 6 3) )) (+ (* 2 3) (- 4 2) ) (+ 6 2) 8 Simple Example (+ (* 2 3) (- 4 (/ 6 3) )) (+ (* 2 3) (- 4 2) ) (+ 6 2) 8 cs3180(Prasad) L12Scm

Lists Ordered sequence of elements of arbitrary type (Heterogeneous) Empty List : () 3-element list : (a b 3) Nested list : (1 (2.3 x) 4) Sets vs lists Duplication matters: (a) =/= (a a) Order matters: (a b) =/= (b a) Nesting-level matters: ( a ) =/= ( (a) ) cs3180(Prasad) L12Scm

Key Point Syntax of Scheme programs has been deliberately designed to match syntax of lists -- its most prominent data structure Important consequence: Supports meta-programming Enables program manipulating programs cs3180(Prasad) L12Scm

Special Forms Definition Conditional (define <var> <expr>) E.g., (define false #f) Conditional (if <test> <then> <else>) E.g., (if (number? 5) (zero? “a”) (/ 10 0) ) define takes the first argument as is. Need: do not evaluate all the arguments. conditional evaluates either the then-expr or the else-expr but not both. (if ’() ’1 ’2) => 1 (if (symbol? 'a) (zero? 5) (/ 1 0)) => #f cs3180(Prasad) L12Scm

Symbols Identifiers treated as primitive values. Distinct from identifiers that name variables in program text. Distinct from strings (sequence of characters). Some meta-programming primitives quote symbol? cs3180(Prasad) L12Scm

Symbolic Data : quote function “say your name aloud” “say ‘your name’ aloud” variable vs symbolic data (to be taken literally) (define x 2) x = 2 (quote x) = x ’x = x (+ 3 6) = 9 ’(+ 3 6) -> list value cs3180(Prasad) L12Scm

Pairs (cons ’a ’b) (cons ’a (cons ’b ’()) ) Printed as: (a . b) a b cs3180(Prasad) L12Scm

(cont’d) (cons ’a (cons ’a (cons ’b ’()))) Printed as: (a a b) a () a cs3180(Prasad) L12Scm

List Functions Operations Expressions car, cdr, cons, null?, ... list, append, ... cadr, caddr, caaar, ... Expressions ( length (quote (quote a)) ) ( length ’’’a ) = 2 ( length ’’’’’’’quote ) (cadar X) = (car (cdr (car X))) The first implementation of LISP was on an IBM 704 by Steve Russell, who read McCarthy's paper and coded the eval function he described in machine code. The familiar (but puzzling to newcomers) names CAR and CDR used in LISP to describe the head element of a list and its tail, evolved from two IBM 704 assembly language commands: Contents of Address Register and Contents of Decrement Register, each of which returned the contents of a 15-bit register corresponding to segments of a 36-bit IBM 704 instruction word. cs3180(Prasad) L12Scm

{ allocate storage for the list and initialize xl} (define xl ’(a b c)) { allocate storage for the list and initialize xl} car, first : list -> element (car xl) = a cdr : list -> list (cdr xl) = (b c) { non-destructive } cons : element x list -> list (cons ’a ’(b c)) = (a b c) car: contents of address register cdr : contents of decrement register cons: construct IBM 704 m/c implementation cs3180(Prasad) L12Scm

(cons (car xl) (cdr xl)) = xl For all non-empty lists xl: (cons (car xl) (cdr xl)) = xl For all x and lists xl: (car (cons x xl)) = x (cdr (cons x xl)) = xl car : first element of the outermost list. cdr : list that remains after removing car. (cons ’() ’()) = ?? (cons ’() ’()) = (()) Wrong alternatives: () (()()) cs3180(Prasad) L12Scm

null? : list -> boolean (null? ’()) = #t (null? ’(a)) = #f (null? 25) = #f list : elements x ... -> list (list ’a ’(a) ’ab) = (a (a) ab) append : list x ...-> list (append ’() (list ’a) ’(0)) = (a 0) { variable arity functions } cs3180(Prasad) L12Scm

Role of parentheses Program Data Extra call to interpreter (list ’append) = (append) (list (append)) = (()) (list (append) (append)) = ? = (()()) Data Extra level of nesting (car ’(a)) = a (car ’((a))) = (a) (list append append) = ? = (@fn @fn) cs3180(Prasad) L12Scm

Equivalence Test (define a (cons 3 ’())) (define b (cons 3 ’())) (eq? (cons 3 ’()) (cons 3 ’())) = #f (define a (cons 3 ’())) (define b (cons 3 ’())) (eq? a b) = #f (define c a) (eq? a c) = #t cs3180(Prasad) L12Scm

Equivalent Scheme Expressions ( (car (list append list)) (cons ’a ’()) ’(1 2 3) ) = ( append ’(a) ’(1 2 3) ) = ’(a 1 2 3) ( (cdr (cons car cdr)) (cons ’car ’cdr) ) = ( cdr ’(car . cdr) ) = ’cdr cs3180(Prasad) L12Scm