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

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

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

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

Lecture 19: Functions, Types and Data Structures in Haskell

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

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

A general introduction to Functional Programming using Haskell

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

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

INTRODUCTION TO HASKELL

Higher Order Functions in Haskell

Standard prelude. Appendix A. A.1 Classes

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

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

CS 440: Programming Languages and Translators, Spring 2019 Mon

A Sudoku Solver Pruning (3A)

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

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

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

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

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

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

Shell CSCE 314 TAMU. Haskell 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

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

Lecture 4: Higher Order Functions

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

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

CSCE 314 Programming Languages

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

Shell CSCE 314 TAMU. Higher Order Functions

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

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

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

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

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

Programming Languages Fall 2013

Functional Programming and Haskell

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

Monad Background (3A) Young Won Lim 10/5/17

CSCE 314 Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CSE 3302 Programming Languages Lecture 8: Functional Programming

CS 457/557: Functional Languages

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

Programming Paradigms

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

Side Effects (3A) Young Won Lim 1/13/18

GHCi: Getting started (1A) Young Won Lim 6/3/17

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

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

Libraries (1A) Young Won Lim 6/5/17

Exercise 1 ( = 24 points)

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

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

Haskell through HUGS THE BASICS

Imperative languages

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Advanced features of Functional Programming (Haskell)

Informatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh

Haskell An Introduction

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

List Functions, and Higher-Order Functions

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

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Side Effects (3B) Young Won Lim 11/20/17

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

Side Effects (3B) Young Won Lim 11/23/17

CSci 4223 Principles of Programming Languages

Side Effects (3B) Young Won Lim 11/27/17

Logic - CM0845 Introduction to Haskell

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18)

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

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

Lists. Michael P. Fourman. February 2, 2010

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

CSC324 Principles of Programming Languages

Introduction to Functional Programming in Haskell 1 / 56

Functional Programming in Haskell for A level teachers

Set Haskell Exercises

Background Constructors (1A) Young Won Lim 7/9/18

Principles of Programming Languages

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Tentamen Functioneel Programmeren 2001 Informatica, Universiteit Utrecht Docent: Wishnu Prasetya

The Haskell HOP: Higher-order Programming

Maybe Monad (3B) Young Won Lim 1/3/18

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

Box-and-arrow Diagrams

CSc 372 Comparative Programming Languages

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.

CS 209 Functional Programming

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

Maybe Monad (3B) Young Won Lim 12/21/17

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

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

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

User Defined Types (1) Type Constructor data Bool = True False A nullary constructor: takes no arguments Data Constructor A multi-constructor The type being defined here is Bool, and it has exactly two values: True and False. data Color = Red Green Blue Type Constructor Data Constructor data Point a = Pt a a A unary constructor (one argument a) A single constructor Pt :: a -> a -> Point a Pt 2.0 3.0 Pt 'a' 'b' Pt True False :: Point Float :: Point Char :: Point Bool https://www.haskell.org/tutorial/goodies.html 4

User Defined Types (2) data Polynom = Poly Float Float Float data the keyword Polynom the name of the data type Poly the constructor function (:t Poly) Float the three arguments to the Poly constructor 5

User Defined Types (3) data Polynom = Poly Float Float Float roots2 :: Polynom -> (Float, Float) roots (Poly a b c) = function definition p1, p2 :: Polynom P1 = Poly 1.0, 2.0, 3.0 P2 = Poly 1.0, 3.0, (-5.0) 6

Recursive Definition of Lists data [a] = [ ] a : [a] Type Constructor Data Constructor Any type is ok but The type of every element in the list must be the same List = [ ] (a : List) an empty list a list with at least one element [ ] (x:xs) https://www.haskell.org/tutorial/goodies.html 7

Parameterized Data Types Parameter data List a = L a (List a) Empty L1, L2, L3 :: List Integer L1 = Empty L2 = L 1 L1 L3 = L 5 L2 L4 = L 1.5 Empty :: List Double Constructor a (a) 8

Polymorphic Type types that are universally quantified in some way over all types essentially describe families of types (forall a) [a] is the family of types consisting of, for every type a, the type of lists of a. lists of integers (e.g. [1,2,3]) lists of characters (['a','b','c']) lists of lists of integers, etc. [2,'b'] is not a valid example https://www.haskell.org/tutorial/goodies.html 9

Subset Polymorphism roots :: (Floating a) => (a, a, a) -> (a, a) https://www.haskell.org/tutorial/goodies.html 10

Parameterized Polymorphism plus :: a -> a -> a, plus :: Int -> Int -> Int, plus :: Rat -> Rat -> Rat, data List a = L a (List a) Empty listlen :: List a -> Int listlen Empty = 0 listlen (L _ list) = 1 + listlen list https://www.haskell.org/tutorial/goodies.html 11

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 12

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 13

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 14

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 15

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. 16

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) 17

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 18

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 19

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 20

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 21

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 22

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

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 24

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] http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 25

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 26

Map as a list comprehension map f xs = [ f x x <- xs] http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 27

Filter as a list comprehension filter p xs = [x x <- xs, p x] http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html 28

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

Type Class Defining a type class by specifying a set of function or constant names, together with their respective types, that must exist for every type that belongs to the class https://en.wikipedia.org/wiki/type_class 30

Type Class Definition a type class Eq intended to contain types that admit equality would be declared in the following way: class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool type a belongs to type class Eq if functions named (==) and (/=) are defined https://en.wikipedia.org/wiki/type_class 31

Class Constraint define a function elem to determines if an element is in a list elem :: (Eq a) => a -> [a] -> Bool elem y [] = False elem y (x:xs) = (x == y) elem y xs the function elem has the type a -> [a] -> Bool with the context (Eq a), constrains the types which a can range over to those a which belong to the Eq type class (Note: Haskell => can be called a 'class constraint'.) https://en.wikipedia.org/wiki/type_class 32

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