Higher Order Functions in Haskell

Similar documents
HIGHER-ORDER FUNCTIONS

CS 440: Programming Languages and Translators, Spring 2019 Mon

Haskell Overview II (2A) Young Won Lim 8/23/16

Haskell Overview II (2A) Young Won Lim 8/9/16

Title: Recursion and Higher Order Functions

Haskell Overview II (2A) Young Won Lim 9/26/16

Standard prelude. Appendix A. A.1 Classes

Lecture 4: Higher Order Functions

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Lambda expressions, functions and binding

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

Watch out for the arrows. Recollecting Haskell, Part V. A start on higher types: Mapping, 1. A start on higher types: Mapping, 2.

EDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read.

A general introduction to Functional Programming using Haskell

Intro to Haskell Notes: Part 5

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

INTRODUCTION TO HASKELL

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Haskell Revision and Exercises

Lecture 19: Functions, Types and Data Structures in Haskell

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

CS 209 Functional Programming

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

A tour of the Haskell Prelude

CSCE 314 Programming Languages

Shell CSCE 314 TAMU. Higher Order Functions

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

An introduction introduction to functional functional programming programming using usin Haskell

CSci 4223 Principles of Programming Languages

301AA - Advanced Programming

Shell CSCE 314 TAMU. Haskell Functions

Functional Programming in Haskell for A level teachers

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

Haskell An Introduction

ML Built-in Functions

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

CSCE 314 Programming Languages

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

Introduction to Functional Programming in Haskell 1 / 56

CS115 - Module 9 - filter, map, and friends

Scheme Quick Reference

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CS 320: Concepts of Programming Languages

Scheme Quick Reference

Functional Programming TDA 452, DIT 142

Programming Paradigms

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

CS 11 Haskell track: lecture 1

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

Logical Methods in... using Haskell Getting Started

Lecture 10 September 11, 2017

301AA - Advanced Programming [AP-2017]

Functional Programming Mid-term exam Tuesday 3/10/2017

Lazy Functional Programming in Haskell

INTRODUCTION TO FUNCTIONAL PROGRAMMING

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Box-and-arrow Diagrams

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

Informatics 1 Functional Programming Lecture 5. Function properties. Don Sannella University of Edinburgh

Parallel Haskell on MultiCores and Clusters

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

INDEX. Symbols & Numbers. Learn You a Haskell for Great Good! 2011 by Miran Lipovača

CS 320: Concepts of Programming Languages

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Exercise 1 ( = 24 points)

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

Functional Languages. Hwansoo Han

Continuation Passing Style. Continuation Passing Style

Programming Language Concepts: Lecture 14

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Haskell Refresher Informatics 2D

Principles of Programming Languages

Functional Programming and Haskell

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

Introduction. chapter Functions

CS 360: Programming Languages Lecture 10: Introduction to Haskell

CSE 3302 Programming Languages Lecture 8: Functional Programming

Chapter 5: The Untyped Lambda Calculus

Programming Systems in Artificial Intelligence Functional Programming

Introductory Example

Logic - CM0845 Introduction to Haskell

Introduction to the Lambda Calculus

CS 457/557: Functional Languages

Programming Languages Fall 2013

Programming with Math and Logic

Chapter 15 Functional Programming Languages

Principles of Programming Languages

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Advanced features of Functional Programming (Haskell)

Functional Programming TDA 452, DIT 142

Transcription:

Higher Order Functions in Haskell Evan Misshula 2018-09-10

Outline Curried Functions Curried comparison Example partial application partial application of a string function Returned functions ZipWith flip Maps and Filters Filter Largest Divisible Lambdas Church and Turing Lambda Calculus Example of a lambda Folds Folds for elem

Curried Functions Every function in haskell only takes one argument But what about max or min? We actually apply parameters to functions one at time These are called "curried" functions This is after Haskell Curry max (Ord a) => a -> a -> a max (Ord a) => a -> (a -> a) If we call a function with to few parameters we get back a partially applied function :set +m -- multthree :: (Num a) => a -> a -> a -> a multthree x y z = x * y * z ((multthree 3) 5) 9 multthree 3 5 9 i==1

Curried comparison Here is a curried comparison These are the same because x is on both sides of the equation comparewithhundred :: (Num a, Ord a) => a -> Ordering comparewithhundred x = compare 100 x comparewithhundred1 :: (Num a, Ord a) => a -> Ordering comparewithhundred1 = compare 100

Example partial application Let s look at an infix function simply surround the function with parentheses and only supply one of the parameters this is called sectioning dividebyten :: (Floating a) => a -> a dividebyten = (/10)

partial application of a string function String functions can be partially applied too this is written in point free style it is also sectioned isupperalphanum :: Char -> Bool isupperalphanum = ( elem [ A.. Z ])

Returned functions Functions can return functions take a function and apply it twice applytwice :: (a -> a) -> a -> a applytwice f x = f (f x)

ZipWith We are going to implement ZipWith It joins two lists and performs a function on the corresponding elements zipwith :: (a -> b -> c) -> [a] -> [b] -> [c] zipwith _ [] _ = [] zipwith [] = [] zipwith f (x:xs) (y:ys) = f x y : zipwith f xs ys

flip flip changes the order of the arguements flip :: (a -> b -> c) -> (b -> a -> c) flip f = g where g x y = f y x flip :: (a -> b -> c) -> b -> a -> c flip f y x = f x y

Maps and Filters Map map takes a function applies the function to each element of a list map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = f x : map f xs

Filter Filter filter take a function called a predicate and a list of any type the predicate takes an element of the list and returns a Bool the filter returns elements for which the predicate is True filter :: (a -> Bool) -> [a] -> [a] filter _ [] = [] filter p (x:xs) p x = x : filter p xs otherwise = filter p xs

Largest Divisible Find the largest number divisible by 3289 under 100,000 We are going to use head and filter and a range largestdivisible :: (Integral a) => a largestdivisible = head (filter p [100000,99999..]) where p show (largestdivisible) 1==1

Lambdas Lambdas are anonymous functions These are unnamed functions They are passed as parameters to other functions They work like composition in math They are called lambdas because of the lambda calculus

Lambda Calculus

Lambda Calculus Lambda Calculus is a formal system for computation it is equivelent to calculation by Turing Machine invented by Alonzo Church in the 1930 s Church was Turing s thesis advisor a function is denoted by the greek letter λ a function f(x) that maps x -> f(x) is: λx.y

Example of a lambda We can pass a lambda to ZipWith a lambda function in Haskell starts with \ can t define several parameters for one para,eters zipwith (\a b -> (a * 30 + 3) / b) [5,4,3,2,1] [1,2,3,4,5] 1==1

Folds Folds encapsulate several functions with (x:xs) patterns they reduce a list to a single value foldl is the left fold function sum :: (Num a) => [a] -> a sum xs = foldl (\acc x -> acc + x) 0 xs sum :: (Num a) => [a] -> a sum = foldl (+) 0 1==1

Folds for elem creating elem we can have a boolean accumulator function elem :: (Eq a) => a -> [a] -> Bool elem y ys = foldl (\acc x -> if x == y then True else acc) 1==1

Right folds foldr works the same way it eats values from the right hand side folds can be used to implement any function that goes through a list once foldl1 and foldr1 same but start at 0 map :: (a -> b) -> [a] -> [b] map f xs = foldr (\x acc -> f x : acc) [] xs 1==1

Scan scanl and scanr are like foldl and foldr only they report the intermediate values scanl (+) 0 [3,5,2,1] scanr (+) 0 [3,5,2,1] scanl1 (\acc x -> if x > acc then x else acc) [3,4,5,3,7,9,2 scanl (flip (:)) [] [3,2,1] 1==1

Function application Function application with $ $ is called the function application changes to right association keeps us from writing parentheses map ($ 3) [(4+), (10*), (^2), sqrt, (^3)] 1==1 [7.0,30.0,9.0,1.7320508075688772,27.0*** Exception: <interac

Function composition Function composition is just like math In math f g(x) = f (g(x)) Let s look at Haskell function g takes a -> b f takes b -> c

Function composition Function composition is just like math In math f g(x) = f (g(x)) Let s look at Haskell function g takes a -> b f takes b -> c so the composition take f. g takes a -> c (.) :: (b -> c) -> (a -> b) -> a -> c f. g = \x -> f (g x)

Function composition examples Function composition examples with a λ with point free notation map (\x -> negate (abs x)) [5,-3,-6,7,-3,2,-19,24] map (negate. abs) [5,-3,-6,7,-3,2,-19,24] 1==1 [-5,-3,-6,-7,-3,-2,-19,-24*** Exception: <interactive>:59:1- [-5,-3,-6,-7,-3,-2,-19,-24*** Exception: <interactive>:59:1-

Multiparameter function composition What if your function takes more than one argument you will need to rewrite sum (replicate 5 (max 6.7 8.9)) (sum. replicate 5. max 6.7) 8.9 sum. replicate 5. max 6.7 $ 8.9 i==1 44.5 44.5 44.5

More point free point free functions sum :: (Num a) => [a] -> a sum xs = foldl (+) 0 xs i==1 <interactive>:111:1-27: error: No instance for (Show ([Integer] -> Integer)) arising from a use of print (maybe you haven t applied a function to enough argu In a stmt of an interactive GHCi command: print it

More point free II point free functions fn x = ceiling (negate (tan (cos (max 50 x)))) fn = ceiling. negate. tan. cos. max 50 i==1