CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.

Slides:



Advertisements
Similar presentations
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
Advertisements

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.
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 425: Semantic Analysis Semantic Analysis Allows rigorous specification of a program’s meaning –Lets (parts of) programming languages be proven correct.
CSE 425: Semantics II Implementing Scopes A symbol table is in essence a dictionary –I.e., every name appears in it, with the info known about it –Usually.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
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.
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.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
CSE 425: Intro to Programming Languages and their Design A Few Key Ideas No particular language is a prerequisite for this course –However you should be.
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.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
Functional Programming in Scheme
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
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.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
CSE 425: Syntax II Context Free Grammars and BNF In context free grammars (CFGs), structures are independent of the other structures surrounding them Backus-Naur.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming CS331 Chapter 14. Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data.
CSE 425: Syntax I Syntax and Semantics Syntax gives the structure of statements in a language –Allowed ordering, nesting, repetition, omission of symbols.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
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.
Comparative Programming Languages Functional programming with Lisp/Scheme.
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.
CS314 – Section 5 Recitation 9
Programming Languages Third Edition
Functional Programming
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming
History of Computing – Lisp
Names and Attributes Names are a key programming language feature
Introduction to Scheme
CS 326 Programming Languages, Concepts and Implementation
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
COP4020 Programming Languages
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Iteration Implemented through loop constructs (e.g., in C++)
CS 36 – Chapter 11 Functional programming Features Practice
The Metacircular Evaluator (Continued)
Delayed Evaluation Special forms in Scheme (e.g., if and cond) do not use applicative order evaluation Only one of two or more expressions is actually.
CSE 3302 Programming Languages
Functional Programming: Lisp
Chapter 15 Functional Programming 6/1/2019.
Presentation transcript:

CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s domain with a set of output values from the function’s range –E.g., written as y = f(x) or f : X → Y … … where X is the domain, Y is the range, x  X is the independent variable, and y  Y is the dependent variable No assignment, so no loops in purely functional code –Instead, rely on recursion to perform iteration Referential transparency –Function’s value depends only on arguments (+ global state) Value semantics –No local state

CSE 425: Functional Programming I Intro to Lambda Calculus Syntax expression → constant | variable | ‘(‘ expression expression ‘)’ | ‘(‘ ‘λ’ variable ‘.’ expression ‘)’ Lambda expressions: function, parameters, arguments –E.g., (λx. + 1 x), written (lambda (x) (+ 1 x)) in Scheme Can apply a lambda expression (“beta reduction”) –E.g., (λx. + 1 x) 2 replaces x with 2, evaluates to 3 Can parameterize an expression (“beta astraction”) –E.g., can parameterize expression (+ 1 2) as (λx. + 1 x) 2 Can also rename variables (“alpha conversion”) and remove redundant expressions (“eta conversion”)

CSE 425: Functional Programming I Intro to Scheme (a Dialect of Lisp) General syntactic structure of Scheme is very simple expression → atom | ‘(‘ {expression} ‘)’ atom → literal | symbol literal → number | string | character | boolean Atomic literals evaluate to themselves –100, “world”, #\z, #T, #f, Symbols are treated as identifiers –Which are then looked up in a symbol table for the current environment, replaced by the values that are found there –If a symbol names a function, it is applied to other values

CSE 425: Functional Programming I Expressions in Scheme All are either special forms or function applications –Special forms begin with a Scheme keyword (e.g., car, cdr, cond, cons, define, display, if, lambda, let, letrec, quote, etc.) –Function application (call) is prefix: name then arguments Predefined operators for many basic functions –Such as + (addition), * (multiplication), / (division), etc. Selection expressions for if, if-else, and if-elseif logic –Use if form for single selection, vs. cond form for multiple Binding lists (using the let or letrec keywords) –Associate values with variables before applying a function Lambda expressions (using the lambda keyword) –Define formal parameter lists for (anonymous) functions

CSE 425: Functional Programming I Data Structures in Scheme Basic construct is a list node (box and arrow notation) –Lists are concatenations of list (or list of list …) nodes –Functions car and cdr select the head vs. the rest of a list The cons function constructs a list from two others –Concatenates them with the first in front of the second The null? primitive tests whether or not a list is empty –Useful for recursive operations on lists (“cdr down, cons up”) L car Lcar cdr Lcdr L “hello, ”“world!” cdr cdr L

CSE 425: Functional Programming I Evaluation, Type Checking, and Scopes Scheme uses applicative order evaluation –Arguments evaluated first, then function is applied to them –Recursively, expression tree is evaluated leaves-to-root Special forms use delayed evaluation –E.g., else part of a cond expression may not evaluate at all Can prevent evaluation using the quote keyword –Identifies a list as being a literal rather than an expression Scheme type checks only when absolutely necessary –E.g., just before a primitive function is applied Scoping is static in Scheme, with hiding with nesting –E.g., an inner let hides an outer let for same symbol name

CSE 425: Functional Programming I Today’s Studio Exercises We’ll explore ideas from Scott Chapter –Looking at basic functional programming features in C++ –Using them in ways similar to Scheme/Lisp features Today’s required exercises are again in C++ –Please take advantage of the on-line tutorial and reference manual pages that are linked in the lab machines –As always, please ask us for help as needed When done, your answers to the course account with subject line “Functional Programming Studio I” –Send to