SchemeCOP-40201 Introduction to Scheme. SchemeCOP-40202 Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
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.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
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).
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
1 Scheme Although Scheme is syntactically a simple language, it supports sophisticated modeling: higher-order functions are a powerful tool in describing.
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Rahman Lavaee Mashhadi Mohammad Shadravan. Conditional expressions LISP was the first language to contain a conditional expression In Fortran and Pascal.
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
A Scheme Refresher (Functional Subset) Prabhaker Mateti.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
ISBN Chapter 15 Functional Programming Languages.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
Functional Programming: Lisp MacLennan Chapter 10.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Ada, Scheme, R Emory Wingard. Ada History Department of Defense in search of high level language around Requirements drafted for the language.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
Operational Semantics of Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
Functional Programming Languages
Functional Programming
History of Computing – Lisp
Section 15.4, 15.6 plus other materials
ML: a quasi-functional language with strong typing
6.001 SICP Object Oriented Programming
Introduction to Scheme
Scheme : variant of LISP
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Env. Model Implementation
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Abstract Syntax Prabhaker Mateti 1.
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture #9 מבוא מורחב.
Abstract Syntax cs7100 (Prasad) L7AST.
6.001 SICP Variations on a Scheme
Announcements Quiz 5 HW6 due October 23
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
Modern Programming Languages Lecture 18 Fakhar Lodhi
topics interpreters meta-linguistic abstraction eval and apply
More Scheme CS 331.
Presentation transcript:

SchemeCOP Introduction to Scheme

SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple uniform syntax; symbols and lists –block structure; static scoping –statement : evaluated for its effect –expression : evaluated for its value Dynamic type checking –flexible but inefficient (rapid prototyping)

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

SchemeCOP Scheme Identifiers E.g., y, x5, +, two+two, zero?, etc (Illegal) 5x, y)2, ab c, etc Identifiers –reserved keywords –variables pre-defined functions/constants ordinary functions = procedures

SchemeCOP Procedure Call (application) (operator-expr operand-expr...) –prefix expression (proc/op arg1 arg2 arg3...) 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. (+ x (p 2 3)) ((f 2 3) 5 6)

SchemeCOP Special Forms Definition (define ) Conditional (if ) (define false #f) (if (zero? 5) 0 #t)

SchemeCOP Data Types values, operations, canonical representation Type-checking static : compile-time : efficient dynamic : run-time : flexible –numbers: +, -, *, number?, = etc –booleans: #t, #f, boolean?, etc –strings: string?, string->list, etc

SchemeCOP Symbols Identifiers treated as primitive values. Manipulated at run-time. –Distinct from identifiers that name variables in the program text. –Distinct from strings (sequence of characters). Meta-programming quote symbol?

SchemeCOP Lists Ordered sequence of elements of arbitrary types (Heterogeneous) operations –car, cdr, cons, null?,... –list, append,... –first, second,..., ninth

SchemeCOP Pairs (cons ’a ’b) (cons ’a (cons ’b nil) ) a b ab nil ()

SchemeCOP Equivalence Test (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

SchemeCOP Vectors Both records and arrays provide random access to components. However, records are heterogeneous, while arrays are homogenoeus. Vectors are heterogeneous structures that provide random access to components using a computable index.

SchemeCOP Constructors and accessors (define v (vector 1 (+ 1 2))) #(1 3) (vector-ref v 0) 1 (vector-length v) 2 Index is 0-based.

SchemeCOP Procedures In Scheme, procedures are first-class objects. That is, they may be passed to or returned from procedures or stored in a data structure. (if (procedure? 3) car cdr) # (procedure? append) #t

SchemeCOP ( (if (procedure? procedure?) car cdr) (cons cdr car)) ’(list append)) = ( (car (cons cdr car)) ’(list append)) = (cdr ’(list append)) = (append)

SchemeCOP Apply -function (apply cons ’( x (y z))) = (cons ’x ’(y z)) = (x y z) (apply f ’(a1 a2... an)) = (f ’a1 ’a2... ’an) (apply )

SchemeCOP (apply apply (list procedure? (list apply))) = (apply apply [ proc-fn [ apply-fn ] ] ) = (apply proc-fn [apply-fn] ) = #t

SchemeCOP Anonymous Fucntions (lambda ) E.g., ( (lambda (n) (+ n 2)) 5) = 7 Evaluate actual argument expressions Bind these values to formals Evaluate body expression (static scoping)

SchemeCOP Variable Arity Procedures ( ) (append ’(1 (p q)) () ’(a b c)) (list ) (lambda ) is bound to the list of actual argument values supplied in a call.

SchemeCOP (define mul (lambda x (if (null? x) 1 (* (car x) (apply mul (cdr x)) ) )) ; 1 is identity w.r.t * ) ; assuming * is binary (mul 1 (+ 2 3) 5)

SchemeCOP Binding constructs in Scheme define binds value to a name. -function application binds formal parameters to actual argument values. let -constructs introduces local bindings –let –let* –letrec

SchemeCOP let -construct ( let ( (var1 exp1) … (varn expn)) exp ) exp1 to expn are evaluated in the surrounding context. var1,…,varn are visible only in exp. >(let ( (x 2) (y 7) ) y) >7

SchemeCOP >(let ( (x y ) (y 7) ) y) >*error* “ y ” undefined >(define y 5) >(let ( (x y ) (y 7) ) y) >7 >(let ( (x y ) (y 7) ) x) >5 >(let ( (y 7) (x y ) ) x) >5 (not 7)

SchemeCOP >(define y 5) >(let ( (y 7) (x y ) ) x) >5 >(let ( (y 7) ) (let ( (x y ) ) x) ) >7 >(let* ( (y 7) (x y) ) x) >7 let* abbreviates nested- let s. Recursive and mutually recursive functions cannot be defined using let and let*.

SchemeCOP letrec -construct ( letrec ( (var1 exp1) … (varn expn)) exp ) var1,…,varn are visible in exp1 to expn in addition to exp. > (letrec ( (x (lambda() y) (y (lambda() x) ) x )

SchemeCOP letrec -construct ( ) >(letrec ( (f (lambda(n) (if (zero? n) 1 (f (- 1 n)) )) ) ) (f 5) ) >1 >(letrec ( ( f (lambda () g) ) ( g 2 ) ) ( f ) ) >2

SchemeCOP boolean connectives (or test1 test2 … testn) (and test1 test2 … testn) or and and are not Scheme procedures. They use short circuit evaluation rather than traditional call-by-value.

SchemeCOP Branching constructs (cond (test1 exp1) (test2 exp2) … (testn expn) (else exp) ) (case key ( keylist1 exp1) ( keylist2 exp2) … ( keylistn expn) (else exp) )