Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.

Slides:



Advertisements
Similar presentations
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Advertisements

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.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
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.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Functional Programming Language (Introduction and Scheme) Lecture 17 – Functional Programming, Spring.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Functional Programming Languages
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
ISBN Chapter 15 Functional Programming Languages.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
ISBN Chapter 15 Functional Programming Languages.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
1 (Functional (Programming (in (Scheme)))) Jianguo Lu.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part I: Concepts and Scheme Programming Languages: Principles and Practice, 2nd Ed. Kenneth.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Functional Programming in 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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Chapter Fifteen: Functional Programming Languages Lesson 12.
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
ISBN Chapter 15 Functional Programming Languages.
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
ISBN Chapter 15 Functional Programming Languages.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
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.
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.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
ISBN Chapter 15 Functional Programming Languages.
CS314 – Section 5 Recitation 9
Functional Programming
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming Languages
Functional Programming
History of Computing – Lisp
CS 326 Programming Languages, Concepts and Implementation
Introduction to Scheme
Chapter 15 – Functional Programming Languages
Introduction to Functional Programming in Racket
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
CS 36 – Chapter 11 Functional programming Features Practice
6.001 SICP Variations on a Scheme
Introduction to Functional Programming in Racket
Announcements Quiz 5 HW6 due October 23
Functional Programming: Lisp
COP4020 Programming Languages
More Scheme CS 331.
Presentation transcript:

slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme

slide 2 Reading Assignment uMitchell, Chapter 3 u“Why Functional Programming Matters” (linked from the course website) uTake a look at Dybvig’s book (linked from the course website)

slide 3 Scheme uImpure functional language uDialect of Lisp Key idea: symbolic programming using list expressions and recursive functions Garbage-collected, heap-allocated (we’ll see why) uSome ideas from Algol Lexical scoping, block structure uSome imperative features

slide 4 Expressions and Lists uCambridge prefix notation: (f x1 x2 … xn) (+ 2 2) (+ (* 5 4) (- 6 2)) means 5*4 + (6-2) uList = series of expressions enclosed in parentheses For example, ( ) is a list of even numbers The empty list is written () uLists represent both functions and data

slide 5 Elementary Values uNumbers Integers, floats, rationals uSymbols Include special Boolean symbols #t and #f uCharacters uFunctions uStrings “Hello, world” uPredicate names end with ? (symbol? ‘(1 2 3)), (list? (1 2 3)), (string? “Yo!”)

slide 6 Top-Level Bindings udefine establishes a mapping from a symbolic name to a value in the current scope Think of a binding as a table: symbol  value (define size 2) ; size = 2 (define sum ( )) ; sum = ( ) uLambda expressions Similar to “anonymous” functions in ML Scheme: (define square (lambda (x) (* x x))) ML: fun square = fn(x)  x*x –What’s the difference? Is this even valid ML? Why?

slide 7 Functions u( define ( name arguments ) function-body ) (define (factorial n) (if (< n 1) 1 (* n (factorial (- n 1))))) (define (square x) (* x x)) (define (sumsquares x y) (+ (square x) (square y))) (define abs (lambda (x) (if (< x 0) (- 0 x) x))) uArguments are passed by value Eager evaluation: argument expressions are always evaluated, even if the function never uses them Alternative: lazy evaluation (e.g., in Haskell)

slide 8 Expression Evaluation uRead-eval-print loop uNames are replaced by their current bindings x; evaluates to 5 uLists are evaluated as function calls (+ (* x 4) (- 6 2)); evaluates to 24 uConstants evaluate to themselves. ‘red; evaluates to ‘red uInnermost expressions are evaluated first (define (square x) (* x x)) (square (+ 1 2))  (square 3)  (* 3 3)  9

slide 9 Equality Predicates ueq? - do two values have the same internal representation? ueqv? - are two numbers or characters the same? uequal? - are two values structurally equivalent? uExamples (eq ‘a ‘a)  #t (eq )  #f (system-specific) (why?) (eqv )  #t (why?) (eqv “abc” “abc”)  #f (system-specific) (why?) (equal “abc” “abc”)  #t

slide 10 Operations on Lists ucar, cdr, cons (define evens ‘( )) (car evens) ; gives 0 (cdr evens) ; gives ( ) (cons 1 (cdr evens)); gives ( ) uOther operations on lists (null? ‘()); gives #t, or true (equal? 5 ‘(5)) ; gives #f, or false (append ‘(1 3 5) evens); gives ( ) (cons ‘(1 3 5) evens); gives ((1 3 5) ) –Are the last two lists same or different?

slide 11 Conditionals uGeneral form (cond (p1 e1) (p2 e2) … (pN eN)) Evaluate p i in order; each p i evaluates to #t or #f Value = value of e i for the first p i that evaluates to #t or e N if p N is “else” and all p 1 … p N-1 evaluate to #f uSimplified form (if (< x 0) (- 0 x)); if-then (if (< x y) x y); if-then-else uBoolean predicates: (and (e1) … (eN)), (or (e1) … (eN)), (not e)

slide 12 Other Control Flow Constructs uCase selection (case month ((sep apr jun nov) 30) ((feb)28) (else31) ) uWhat about loops? Iteration  Tail recursion Scheme implementations must implement tail- recursive functions as iteration

slide 13 Delayed Evaluation uBind the expression to the name as a literal… (define sum ‘( )) sum  ( ) –Evaluated as a symbol, not a function uEvaluate as a function (eval sum)  6 uNo distinction between code (i.e., functions) and data – both are represented as lists!

slide 14 Imperative Features uScheme allows imperative changes to values of variable bindings (define x `(1 2 3)) (set! x 5) uIs it Ok for new value to be of a different type? Why? uWhat happens to the old value?

slide 15 Let Expressions uNested static scope u(let ((var1 exp1) … (varN expN)) body) (define (subst y x alist) (if (null? alist) ‘() (let ((head (car alist)) (tail (cdr alist))) (if (equal? x head) (cons y (subst y x tail)) (cons head (subst y x tail))))) uThis is just syntactic sugar for a lambda application (why?)

slide 16 Let* u(let* ((var1 exp1) … (varN expN)) body) Bindings are applied sequentially, so var i is bound in exp i+1 … exp N uThis is also syntactic sugar for a (different) lambda application (why?) (lambda (var1) ( (lambda (var2) ( … ( (lambda (varN) (body)) expN) … ) exp1

slide 17 Functions as Arguments (define (mapcar fun alist) (if (null? alist) ‘() (cons (fun (car alist)) (mapcar fun (cdr alist))) )) (define (square x) (* x x)) What does (mapcar square ‘( )) return? ( ) F

slide 18 “Folding” a Data Structure uFolding: processing a data structure in some order to construct a return value Example of higher-order functions in action uSumming up list elements (left-to-right) (foldl + 0 ‘( ))  15 –Evaluates as (+ 5 (+ 4 (+ 3 (+ 2 (+ 1 0)))). Why? (define (sum lst) (foldl + 0 lst)) uMultiplying list elements (right-to-left) (define (mult lst) (foldr * 1 lst)) (mult ‘(2 4 6))  (* (* (* 6 4) 2) 1))  48

slide 19 Using Recursion uCompute length of the list recursively (define length (lambda(lst) (if (null? lst) 0 (+ 1 (length (cdr list)))))) uCompute length of the list using foldl (define length (lambda(lst) (foldl (lambda (_ n) (+ n 1)) 0 lst) ) ) Ignore 1 st argument. Why?

slide 20 Key Features of Scheme uScoping: static uTyping: dynamic (what does this mean?) uNo distinction between code and data Both functions and data are represented as lists Lists are first-class objects –Can be created dynamically, passed as arguments to functions, returned as results of functions and expressions This requires heap allocation (why?) and garbage collection (why?) Self-evolving programs