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

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

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

Haskell Overview III (3A) Young Won Lim 10/4/16

Lecture 4: Higher Order Functions

Haskell-Tutorial. Damir Medak Gerhard Navratil. Institute for Geoinformation Technical University Vienna. February 2003

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

A general introduction to Functional Programming using Haskell

Higher Order Functions in Haskell

Background Operators (1E) Young Won Lim 7/7/18

Lecture 19: Functions, Types and Data Structures in Haskell

State Monad (3D) Young Won Lim 9/25/17

A Sudoku Solver Pruning (3A)

INTRODUCTION TO HASKELL

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

Shell CSCE 314 TAMU. Haskell Functions

An introduction introduction to functional functional programming programming using usin Haskell

CS 440: Programming Languages and Translators, Spring 2019 Mon

CS 457/557: Functional Languages

CSCE 314 Programming Languages

Shell CSCE 314 TAMU. Higher Order Functions

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

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

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

A Sudoku Solver (1A) Richard Bird Implementation. Young Won Lim 11/15/16

Standard prelude. Appendix A. A.1 Classes

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Haskell Overview IV (4A) Young Won Lim 10/24/16

Functional Programming and Haskell

Haskell Overview IV (4A) Young Won Lim 10/13/16

CSE 3302 Programming Languages Lecture 8: Functional Programming

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

Monad Background (3A) Young Won Lim 11/20/17

Polymorphism Overview (1A) Young Won Lim 2/20/18

Box-and-arrow Diagrams

Background Type Classes (1B) Young Won Lim 6/28/18

Monad (1A) Young Won Lim 6/9/17

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

Introduction to Functional Programming in Haskell 1 / 56

CSCE 314 Programming Languages

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

CSci 4223 Principles of Programming Languages

CSc 372 Comparative Programming Languages

HIGHER-ORDER FUNCTIONS

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.

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

Haskell through HUGS THE BASICS

CS 320: Concepts of Programming Languages

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

Background Type Classes (1B) Young Won Lim 6/14/18

CS 11 Haskell track: lecture 1

Monad (1A) Young Won Lim 6/21/17

Lists. Michael P. Fourman. February 2, 2010

Haskell An Introduction

The Haskell HOP: Higher-order Programming

Operators (2A) Young Won Lim 10/2/13

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

Background Functions (1C) Young Won Lim 4/3/18

Exercise 1 ( = 24 points)

Carlos Varela RPI September 19, Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL

Monad (3A) Young Won Lim 8/9/17

Monad (1A) Young Won Lim 6/26/17

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Logic - CM0845 Introduction to Haskell

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

IO Monad (3C) Young Won Lim 8/23/17

Lecture 21: Functional Programming in Python. List Comprehensions

Monad Background (3A) Young Won Lim 11/8/17

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

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

Lecture 2: List algorithms using recursion and list comprehensions

GHCi: Getting started (1A) Young Won Lim 5/26/17

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

Arrays (1A) Young Won Lim 12/4/17

Background Functions (1C) Young Won Lim 5/26/18

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

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

State Monad Example (3H) Young Won Lim 2/16/18

Imperative languages

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Monad Background (3A) Young Won Lim 11/18/17

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

Monoid (4A) Young Won Lim 8/12/17

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

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

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

Binary Search Tree (2A) Young Won Lim 5/17/18

Pointers (1A) Young Won Lim 11/1/17

Lecture 5: Lazy Evaluation and Infinite Data Structures

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Programming Paradigms

02157 Functional Programming Lecture 2: Functions, Basic Types and Tuples

Operators (2A) Young Won Lim 10/5/13

Functional Programming - 2. Higher Order Functions

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

State Monad Examples (6C) Young Won Lim 9/26/18

State Monad (3D) Young Won Lim 8/31/17

Transcription:

(2A)

Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". Please send corrections (or suggestions) to youngwlim@hotmail.com. This document was produced by using OpenOffice.

Based on Haskell Tutorial, Medak & Navratil ftp://ftp.geoinfo.tuwien.ac.at/navratil/haskelltutorial.pdf Yet Another Haskell Tutorial, Daume https://www.umiacs.umd.edu/~hal/docs/daume02yaht.pdf 3

Counting functions l1 [] = 0 l1 (x:xs) = 1 + l1 xs l2 xs = if xs == [] then 0 else 1 + l2 (tail xs) l3 xs xs == [] = 0 otherwise = 1 + l3 (tail xs) l4 = sum. map (const 1) l5 xs = foldl inc 0 xs where inc x _ = x+1 l6 = foldl (\n _ -> n + 1) 0 Pattern Matching if-then-else guard notation replace and sum local function, local counter lambda expression 4

Guard Notation sign x x > 0 = 1 x == 0 = 0 x < 0 = -1 https://www.haskell.org/tutorial/patterns.html if (x > 0) sign = +1; else if (x == 0) sign = 0; else sign= -1 5

Anonymous Function Input Prompt> (\x -> x + 1) 4 5 :: Integer Prompt> (\x y -> x + y) 3 5 8 :: Integer addone = \x -> x + 1 λ x x 2 \ x -> x*x In Lambda Calculus Lambda Expression Lambda Abstraction Anonymous Function https://www.haskell.org/tutorial/patterns.html 6

Naming a Lambda Expression Input inc x = x + 1 inc = \x -> x + 1 add x y = x + y add = \x y -> x + y Lambda Expression https://www.haskell.org/tutorial/patterns.html 7

Lambda Calculus The lambda calculus consists of a language of lambda terms, which is defined by a certain formal syntax, and a set of transformation rules, which allow manipulation of the lambda terms. These transformation rules can be viewed as an equational theory or as an operational definition. All functions in the lambda calculus are anonymous functions, having no names. They only accept one input variable, with currying used to implement functions with several variables. 8

Composite Function (1) (.) :: (b -> c) -> (a -> b) -> (a -> c) f g f(g(x)) https://www.haskell.org/tutorial/patterns.html f. g = \ x -> f (g x) (f. g) x = f (g x) 9

Composite Function (2) p1 = (1.0,2.0,1.0) :: (Float, Float, Float) p2 = (1.0,1.0,1.0) :: (Float, Float, Float) ps = [p1,p2] newps = filter real ps rootsofps = map roots newps RootsOfPs2 = (map roots. filter real) ps 10

Local Variables lend amt bal = let reserve = 100 newbal = bal - amt in if bal < reserve then Nothing else Just newbal lend2 amt bal = if amt < reserve * 0.5 then Just newbal else Nothing where reserve = 100 newbal = bal - amt http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 11

Local Function pluralise :: String -> [Int] -> [String] pluralise word counts = map plural counts where plural 0 = "no " ++ word ++ "s" plural 1 = "one " ++ word plural n = show n ++ " " ++ word ++ "s" http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 12

Local Function roots :: (Float, Float, Float) -> (Float, Float) type PolyT = (Float, Float, Float) type RootsT = (Float, Float) roots :: PolyT -> RootsT typedef http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 13

Infix operators as functions Input (x+) = \y -> x + y Input (+y) = \x -> x + y partial application of an infix operator Inputs (+) = \x y -> x + y https://www.haskell.org/tutorial/functions.html 14

Infix operators as function values inc = (+1) add = (+) map (+) [1,2,3] [(+1),(+2),(+3)] https://www.haskell.org/tutorial/functions.html 15

Sections (+), (*) :: Num a => a -> a -> a? 3 + 4? (+) 3 4 (+) :: Num a => a -> a -> a (*) :: Num a => a -> a -> a a belongs to the Num type class http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 16

Sections (3 +) :: Num a => a -> a? map (3 +) [4, 7, 12]? filter (==2) [1,2,3,4] [2]? filter (<=2) [1,2,3,4] [1,2]? filter (2<) [1,2,3,4] [3,4]? filter (<2) [1,2,3,4] [1]? filter (2>) [1,2,3,4] [1]? filter (>2) [1,2,3,4] [3,4] http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 17

List Comprehensions [x x <- [0..100], odd x] {x x [0,100],odd x}? f [2,3,4] [5,6] where f xs ys = [x*y x <- xs, y <- ys] [10,12,15,18,20,24] :: [Integer] 2 5 3 6 4 http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 18

List Comprehensions [ (i,j) i <- [1,2], j <- [1..3] ] i=1 j=1 i=2 j=2 j=3 [(1,1),(1,2),(1,3),(2,1),(2,2),(2,3)] [h (h:t) <- [[1, 2, 3], [4, 5, 6]]] (h:t) = (1:[2,3]) (h:t) = (4:[5,6]) h = 1, 4 [1, 4] [t (h:t) <- [[1, 2, 3], [4, 5, 6]]] (h:t) = (1:[2,3]) (h:t) = (4:[5,6]) t = [2,3], [5,6] [[2,3], [5,6]] http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 19

List function map & filter as a list comprehension map f xs = [ f x x <- xs ] filter p xs = [x x <- xs, p x ] additional condition to be satisfied http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 20

Currying f x y = blabla f x = \y -> blabla f = \x -> (\y -> blabla) (f x) y f is a function with one argument, x, and returns another function with one argument, y, and then returns the actual result blabla. This is known as currying. http://stackoverflow.com/questions/3794371/haskell-currying-need-further-explanation 21

Curry & Uncurry f :: a -> b -> c g :: (a -> b) -> c curried form uncurried form Currying is the process of transforming a function that takes multiple arguments into a function that takes just a single argument and returns another function if any arguments are still needed. f = curry g f x y g (x, y) currying g = uncurry f g(x, y) f x y uncurrying f x y = g (x, y) https://wiki.haskell.org/currying 22

Functional & Imperative Programming c := 0 for i:=1 to n do c := c + a[i] * b[i] a belongs to the Num type class uncurried form inn2 :: Num a => ([a], [a]) -> a Inn2 = foldr (+) 0. map (uncurry (*)). uncurry zip (1) uncurry zip (2) map (uncurry (*)) (3) foldr (+) 0 http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 23

Simple List Functions zip and unzip functions zip [a1, a2, a3] [b1,b2,b3] [(a1,b1),(a2,b3),(a3,b3)] f x y g (x, y) currying g(x, y) f x y uncurrying uncurry zip ( [a1, a2, a3], [b1,b2,b3] ) [(a1,b1),(a2,b2),(a3,b3)] 24

Simple List Functions zip and unzip functions map (uncurry (*) ) [(a1,b1),(a2,b2),(a3,b3)] [a1*b1, a2*b2, a3*b3] map foldr (+) 0 [a1*b1, a2*b2, a3*b3] a1*b1 + a2*b2 + a3*b3 25

Functional & Imperative Programming a belongs to the Num type class inn2 :: Num a => ([a], [a]) -> a inn2 = foldr (+) 0. map (uncurry (*)). uncurry zip inn2 x = (foldr (+) 0. map (uncurry (*)). uncurry zip) x inn2 x = (foldr (+) 0 (map (uncurry (*)) (uncurry zip x))) testvec = inn2 ([1,2,3], [4,5,6]) uncurried form http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 26

foldr, foldl, fold1 foldr (-) 1 [4,8,5] 4 - (foldr (-) 1 [8,5]) 4 - (8 - foldr (-) 1 [5]) 4 - (8 - (5 - foldr (-) 1 [])) 4 - (8 - (5-1)) 4 - (8-4) 4-4 0 foldl (-) 1 [4,8,5] (foldl (-) 1 [4,8]) - 5 ((foldl (-) 1 [4]) - 8) 5 ((1 4) 8) 5 ((-3) 8) 5-11 -5-16 foldl (+) 0 [4,8,5] foldl1 (+) [4,8,5] foldl (*) 1 [4,8,5] foldl1 (*) [4,8,5] No starting value argument (4 (8 (5))) (4 (8 (5))) (4 (8 (5))) (((4) 8) 5) (((4) 8) 5) (((4) 8) 5) 27

Fold function applications ft = foldr (*) 7 [2,3] ut = uncurry (+) (5,6) zt = zip Haskell [1,2,3,4,5,6,7] (2 (3 * 7)) (2 * 3 *7) (+) 5 6 uncurry (+) (5, 6) innxa, innxb : [[Integer]] -> Integer innxa = foldr (+) 0. map (foldr (*) 1). transpose innxb = foldr1 (+). map (foldr1 (*)). transpose http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 28

Transpose transpose :: [[a]] -> [[a]] transposes the rows and columns of its argument. transpose [[a,b,c],[1,2,3]] == [[a,1],[b,2],[c,3]] elements may be skipped transpose [[10,11],[20,],[],[30,31,32]] == [[10,20,30],[11,31],[32]] [[10,11, - ],[20, -, - ],[ -, -, - ],[30,31,32]] [[10,20, -, 30], [11, -, -, 31], [ -, -, -, 32]] [[10,20, 30], [11, 31], [32]] https://hackage.haskell.org/package/base-4.9.0.0/docs/data-list.html 29

Prepend (cons) Operator : 1 : 2 : 3 : 4 : [] is the list [1, 2, 3, 4]. 4 : [] (cons 4 to the empty list) 3 : [4] (cons 3 onto the list containing 4) 2 : [3,4] (cons 2 onto the list containing 3, 4) 1 : [2,3,4] (cons 1 onto the list containing 2,3,4) http://stackoverflow.com/questions/1696751/what-does-the-infix-operator-do-in-haskell 30

Transpose transpose :: [[a]] -> [[a]] transpose [] = [] transpose ([]:xss) = transpose xss transpose ((x:xs) : xss) = (x : [h (h:t) <- xss]) : transpose (xs : [t (h:t) <- xss]) ((a:[a]) : [[a]]) ((x:xs) : xss) ((x:xs) : (h:t)) ((a:[a]) :([a]:[[a]]) List Comprehension [x x <- [0..100], odd x] {x x [0,100], odd x} (x:h) : transpose (xs:t) (a:[a]) : ([a]:[[a]]) https://hackage.haskell.org/package/base-4.9.0.0/docs/data-list.html 31

Transpose Example (1) [[1,2,3], [4,5,6],[1,1,1]] [1,2,3] : [[4,5,6],[1,1,1]] (1 : [2,3]) : ([4,1] : [[5,6],[1,1]]) (1 : [4,1]) : transpose ( [2,3] : [[5,6],[1,1]]) [1,4,1] transpose ([[2,3],[5,6],[1,1]]) ((a:[a]) : [[a]]) ((x:xs) : xss) [h (h:t) <- xss] [t (h:t) <- xss] ((x:xs) : (h:t)) ((a:[a]) :([a]:[[a]]) (x:h) : transpose (xs:t) (a:[a]) : ([a]:[[a]]) https://hackage.haskell.org/package/base-4.9.0.0/docs/data-list.html 32

Transpose Example (2) ([[2,3],[5,6],[1,1]] [2,3] : [[5,6],[1,1]] (2 : [3]) : ([5,1] : [[6,1]]) (2 : [5,1]) : transpose ( [3] : [[6,1]]) [2,5,1] : transpose ([[3,6,1]]) ((a:[a]) : [[a]]) ((x:xs) : xss) [h (h:t) <- xss] [t (h:t) <- xss] ((x:xs) : (h:t)) ((a:[a]) :([a]:[[a]]) (x:h) : transpose (xs:t) (a:[a]) : ([a]:[[a]]) https://hackage.haskell.org/package/base-4.9.0.0/docs/data-list.html 33

Inner Product innxa, innxb : [[Integer]] -> Integer innxa = foldr (+) 0. map (foldr (*) 1). transpose innxb = foldr1 (+). map (foldr1 (*)). transpose [[1, 2, 3], [10, 20, 30]] [[1, 10], [2, 20], [3, 30]] [1*10, 2*20, 3*30] 1*10 + 2*20 + 3*30 http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 34

1. Terse Syntax Recognize blocks By indentation (prefer to use space than tab) By newline https://www.fpcomplete.com/blog/2012/09/ten-things-you-should-know-about-haskell-syntax 35

2. Function Calls a b c d e f Function name : a Function arguments : b, c, d, e, and f a (b, c, d, e, f) If parenthesis is used, use also comma : tuple representation Function name : a Function argument : one tuple (b, c, d, e, f) https://www.fpcomplete.com/blog/2012/09/ten-things-you-should-know-about-haskell-syntax 36

3. Function Definitions a b c = d e f Function name : a Function arguments : b and c The body of function a is a function call d with argument of e and f a (b, c) = d e f Formal parameters in parentheses : patterns contained f (leaf x) = f (node, left, right) = https://www.fpcomplete.com/blog/2012/09/ten-things-you-should-know-about-haskell-syntax 37

4.Currying a b c d e f function with 5 arguments can be called with fewer arguments a 1.0 2.0 will return a new function say g d e f Partial application is possible because any function with multiple arguments can be curried f :: a -> b -> c f x = g f x y = g y https://www.fpcomplete.com/blog/2012/09/ten-things-you-should-know-about-haskell-syntax 38

5. statement-like expression a function body : expression (return value) No return statement in Haskell But return library function exists If-then-else structure : expression Local variable definitions : expression let x = 5 in x * x Pattern matching constructs : expression case tree of Leaf x ->... Node left right ->... https://www.fpcomplete.com/blog/2012/09/ten-things-you-should-know-about-haskell-syntax 39

6. No Loop map, filter, foldl, and foldr https://www.fpcomplete.com/blog/2012/09/ten-things-you-should-know-about-haskell-syntax 40

7. Function application precedence A b c d + e f g a b c d e f g https://www.fpcomplete.com/blog/2012/09/ten-things-you-should-know-about-haskell-syntax 41

8. Data Types : algebraic, pattern matching data Bool = True False OR : the role of addition AND : the role of multiplication Data constructors used as patterns to match data Tree = Leaf Int Node Tree Tree https://www.fpcomplete.com/blog/2012/09/ten-things-you-should-know-about-haskell-syntax 42

9. No order Order doesn t matter in the definition len x y = sqrt (sq x + sq y) where sq a = a * a can define local functions or variables after the code that calls them https://www.fpcomplete.com/blog/2012/09/ten-things-you-should-know-about-haskell-syntax 43

10. Order in Do do a <- givemeana b <- givemeab return (a + b) https://www.fpcomplete.com/blog/2012/09/ten-things-you-should-know-about-haskell-syntax 44

Tree Example data Tree = Leaf Int Node Tree Tree g (Leaf x) = x g (Node left right) = g left + g right f tree = case tree of Leaf x -> x Node left right -> f left + f right https://www.fpcomplete.com/blog/2012/09/ten-things-you-should-know-about-haskell-syntax 45

References [1] ftp://ftp.geoinfo.tuwien.ac.at/navratil/haskelltutorial.pdf [2] https://www.umiacs.umd.edu/~hal/docs/daume02yaht.pdf