A global environment val a = 15 val b = "foo"; val c = fn (n, p)

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

Closures & Environments CS153: Compilers Greg Morrisett.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
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.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
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.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
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.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
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.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Functional Programming in Scheme and Lisp.
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,
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
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.
Message Passing: Alternative to Generics data is “active” — knows how to compute dispatch on operation (define (rat (n ) (d )) (method ((op )) (cond ((=
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.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
CSE 341 Lecture 21 delayed evaluation; thunks; streams slides created by Marty Stepp
Functional Programming
Functional Programming
Functional Programming Languages
Chapter 11 - Functional Programming, Part I: Concepts and Scheme
Functional Programming
History of Computing – Lisp
Section 15.4, 15.6 plus other materials
Edited by Original material by Eric Grimson
CS 326 Programming Languages, Concepts and Implementation
Modern Programming Languages Lecture 20 Fakhar Lodhi
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Chapter 15 – Functional Programming Languages
COP4020 Programming Languages
Env. Model Implementation
Emily Leland (Not Nick) Spring 2017
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Dynamic Scoping Lazy Evaluation
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
6.001 SICP Further Variations on a Scheme
Adapted from slides by Nicholas Shahan and Dan Grossman
Lecture 12: Message passing The Environment Model
3.6 Interpreter: Recursion
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
6.001 SICP Variations on a Scheme
CSE 3302 Programming Languages
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
Announcements Quiz 5 HW6 due October 23
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
Common Lisp II.
More Scheme CS 331.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Presentation transcript:

A global environment val a = 15 val b = "foo"; val c = fn (n, p) => p = n * p; val d = [1,2];

Let and local environments val a = 20 in hd(d) + a end;

Function expressions generate closures fun f(x, y) = let fun nested(z) = a + x + z in nested(y) end;

Function application 1 f(2, 10)

Nested aplication let fun nested(z) = a + x + z in nested(y) end

Closures as parameters val a = 15; fun addA(k) = k + a; fun doAction(f, a) = f(a);

Closures as return values fun makeAddX(x) = fn y => x + y; val add5 = makeAddX 5; val elevn = add5 6;

lambda = let let val x = 3; val y = 4; in x + y end;

Scheme Expressions: atoms or (parenthesized lists) Evaluator: Parse an expression If expression is (bound to) atom, return it Otherwise, expression is a list with head V: If V is a special form, do the special form Otherwise, evaluate elements in tail and apply V to tail.

Scheme examples (define a 15) ; val a = 15 (define d '(1 2)) ; val d = [1, 2] (let* ((a 20)) (+ (car d) a)) let val a = 20 in hd(d) + a; (define (f x y) (let* ((nested (lambda (z) (+ a x z)))) (nested y))) fun f(x, y) = let val nested = fn z => a + x + z; in nested y;

Scheme dynamic typing Types are "latent" Types of values are irrelevant (unchecked) until you use them. Typing is still strong: attempting to perform an invalid operation will raise a runtime type error

Objects are higher-order functions (define (make-student name id-num) (lambda (message) (cond ((equal? message 'getName) name) ((equal? message 'getID) id-num) ((equal? message 'cloneMe) (make-student (name id-num))) (else "INVALID MESSAGE")))) (define alice (make-student "Alice" 123456)) (display (alice 'getName))

Programs as data (eval (list '+ 1 2 3)) ; Evaluate list structure as if it were ; a program (eval (list '+ 1 2 3)) ; Single quote recursively prevents ; evaluation of sublists (define mystuff '(* (+ 1 2 3)))

Why functional programming? Reason 1: influence on other languages Understand languages of present Modern languages borrow heavily from ideas invented in functional languages Be prepared for languages of the future Not everyone retires by 35 New languages come along all the time Learn concepts once, reuse anywhere

Why functional programming? Reason 2: Functional style enhances clarity (when used appropriately) Avoid side effects Prefer return values to output parameters & side effects. Try to preserve "substitutability". Take advantage of constant data. Use higher-order thinking parameterize fns with fns for flexibility and reuse Polymorphic types Think of operations that are operate over "generic" types.

Why functional programming? Reason 3: It's cool. Programming is a medium for expressing ideas Functional languages are powerful, flexible, and expressive. You are not just a code monkey.