Creating Functions Functional Programming. The function calculator Functional programming is all about using functions Functions are first class – Take.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
© M. Winter COSC 4P41 – Functional Programming Patterns of computation over lists Applying to all – mapping map :: (a -> b) -> [a] -> [b] map f.
Cse536 Functional Programming 1 6/10/2015 Lecture #6, Oct 13, 2004 Reading Assignments –Read chapter 5 of the Text Polymorphic and Higher-Order Functions.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Chapter 9 More About Higher-Order Functions. Currying Recall the function: simple n a b = n * (a+b) Note that: simple n a b is really (((simple n) a)
Higher-Order Functions Koen Lindström Claessen. What is a “Higher Order” Function? A function which takes another function as a parameter. Examples map.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
Cse502 Functional Programming 1 6/26/2015 Lecture #11, Nov. 4, 2002 Todays Topics –Using Calculation to prove 2 functions are equal »An example from class.
Advanced Programming Handout 7 More About Higher-Order Functions (SOE Chapter 9)
Chapter 5 Polymorphic and Higher-Order Functions.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 19: Functions, Types and Data Structures in Haskell COMP 144 Programming Language.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Copyright © 2010 Pearson Education, Inc
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Figure 2.1a Evaluating Expressions To evaluate an algebraic expression, we may substitute the value(s) in place of the variable(s) and evaluate the numeric.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Recursion Higher Order Functions CSCE 314 Spring 2016.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
Cs776 (Prasad)L2HOF1 Higher-Order Functions. cs776 (Prasad)L2HOF2 Higher-Order Functions A function that takes a function as argument and/or returns a.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
Click to edit Master text styles Stacks Data Structure.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Functional Programming
Conditional Expressions
Recursion.
Types CSCE 314 Spring 2016.
Functional Programming Lecture 12 - more higher order functions
Functions and patterns
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
Higher-Order Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
CSE 341 Section 5 Winter 2018.
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Higher Order Functions
CSE 341 Section 3 Nick Mooney Spring 2017.
CSE-321 Programming Languages Introduction to Functional Programming
CSE 3302 Programming Languages
Announcements Quiz 5 HW6 due October 23
Functions and patterns
PROGRAMMING IN HASKELL
Higher-Order Functions in Haskell
Functional Programming
PROGRAMMING IN HASKELL
Presentation transcript:

Creating Functions Functional Programming

The function calculator Functional programming is all about using functions Functions are first class – Take as input, return as result, store in data A functional language is a function calculator What buttons do we have for “creating” functions?

12 ways to get a new function By defining one at top level – By equation – By cases – By patterns By local definition (where and let) By use of a library By lambda expression (anonymous functions) By parenthesizing binary operators By section By currying (partial application) By composition By combinator (higher order functions) By using data and lookup (arrays lists and finite functions)

By defining at top level Module Test where plus5 x = x + 5 last x = head(reverse x) CreatingFunctions> plus CreatingFunctions> last [2,3,4] 4

By cases absolute x | x < 0 = -x | x >= 0 = x swap (x,y) | x < y = (x,y) | x > y = (y,x) | x==y = (x,y) Name and argument condition Vertical Bar Equal sign Value for case CreatingFunctions> absolute 3 3 CreatingFunctions> absolute (-4) 4 CreatingFunctions> swap (23,5) (5,23)

By patterns Example on Booleans myand True False = False myand True True = True myand False False = False myand False True = False Order Matters – Variables in Patterns match anything myand2 True True = True myand2 x y = False – What happens if we reverse the order of the two equations above? Pattern may contain constructors. Constructors are always capitalized. True and False are constructors

By local definition (where and let) ordered = sortBy backwards [1,76,2,5,9,45] where backwards x y = compare y x CreatingFunctions> ordered [76,45,9,5,2,1]

By use of a Library smallest = List.minimum [3,7,34,1] CreatingFunctions> smallest 1

By lambda expression (anonymous functions) descending = sortBy (\ x y -> compare y x) [1,76,2,5,9,45] bySnd = groupBy (\ (x,y) (m,n) -> y==n) [(1,'a'),(3,'a'),(2,'c')] CreatingFunctions> descending [76,45,9,5,2,1] CreatingFunctions> bySnd [[(1,'a'),(3,'a')],[(2,'c')]]

By parenthesizing binary operators six:: Integer six = foldr (+) 0 [1,2,3] CreatingFunctions> six 6

By section add5ToAll = map (+5) [2,3,6,1] CreatingFunctions> add5ToAll [7,8,11,6]

By partial application hasFour = any (==4) doubleEach = map (\ x -> x+x) Note, both map and any, each take 2 arguments CreatingFunctions> hasFour [2,3] False CreatingFunctions> hasFour [2,3,4,5] True CreatingFunctions> doubleEach [2,3,4] [4,6,8]

By composition hasTwo = hasFour. doubleEach empty = (==0). length CreatingFunctions> hasTwo [1,3] False CreatingFunctions> hasTwo [1,3,2] True CreatingFunctions> empty [2,3] False CreatingFunctions> empty [] True

By combinator (higher order functions) k x = \ y -> x all3s = map (k 3) [1,2,3] CreatingFunctions> :t k True k True :: a -> Bool CreatingFunctions> all3s [3,3,3]

Using data and lookup (arrays, lists, and finite functions) whatDay x = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"] !! X first9Primes = array (1,9) (zip [1..9] [2,3,5,7,11,13,17,19,23]) nthPrime x = first9Prime ! x CreatingFunctions> whatDay 3 "Wed" CreatingFunctions> nthPrime 5 11

When to define a higher order function? Abstraction is the key mysum [] = 0 mysum (x:xs) = (+) x (mysum xs) myprod [] = 1 myprod (x:xs) = (*) x (myprod xs) myand [] = True myand (x:xs) = (&&) x (myand xs) Note the similarities in definition and in use ? mysum [1,2,3] 6 ? myprod [2,3,4] 24 ? myand [True, False] False

When do you define a higher order function? Abstraction is the key mysum [] = 0 mysum (x:xs) = (+) x (mysum xs) myprod [] = 1 myprod (x:xs) = (*) x (myprod xs) myand [] = True myand (x:xs) = (&&) x (myand xs) Note the similarities in definition and in use ? mysum [1,2,3] 6 ? myprod [2,3,4] 24 ? myand [True, False] False

Abstracting myfoldr op e [] = e myfoldr op e (x:xs) = op x (myfoldr op e xs) ? :t myfoldr myfoldr :: (a -> b -> b) -> b -> [a] - > b ? myfoldr (+) 0 [1,2,3] 6 ?

Functions returned as values Consider: k x = (\ y -> x) ? (k 3) 5 3 Another Example: plusn n = (\ x -> x + n) ? (plusn 4) 5 9 Is plusn different from plus ? why? – plus x y = x + y