An Introduction to Programming in Haskell. Gabriel J. Ferrer Hendrix College

Similar documents
PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

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

Shell CSCE 314 TAMU. Functions continued

Module Title: Informatics 1 Functional Programming (first sitting) Exam Diet (Dec/April/Aug): December 2014 Brief notes on answers:

Part 0. A (Not So) Brief Introduction to Haskell

Standard prelude. Appendix A. A.1 Classes

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

Functional Programming and Haskell

Logic - CM0845 Introduction to Haskell

INTRODUCTION TO HASKELL

Haskell Refresher Informatics 2D

Box-and-arrow Diagrams

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

Programming Paradigms

Lazy Functional Programming in Haskell

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

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

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

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

Imperative languages

JVM ByteCode Interpreter

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

Shell CSCE 314 TAMU. Haskell Functions

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

INTRODUCTION TO FUNCTIONAL PROGRAMMING

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

Lecture 5: Lazy Evaluation and Infinite Data Structures

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

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

Part 1. An Implementation of the Gale-Shapley Algorithm

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

1 The smallest free number

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

Functional Programming in Haskell Part I : Basics

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Functional Programming TDA 452, DIT 142

CSCE 314 Programming Languages

Testing. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]

Shell CSCE 314 TAMU. Higher Order Functions

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

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

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

CSCE 314 Programming Languages. Interactive Programming: I/O

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

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

Programming Languages Fall 2013

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

Introduction to Haskell

CS 209 Functional Programming

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

Monads and all that I. Monads. John Hughes Chalmers University/Quviq AB

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

Introduction to Haskell

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

CS 340 Spring 2019 Midterm Exam

Higher Order Functions in Haskell

Functional Programming TDA 452, DIT 142

Lecture 19: Functions, Types and Data Structures in Haskell

Exercises on ML. Programming Languages. Chanseok Oh

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

CS 457/557: Functional Languages

Topic of today: memoization. The string alignment problem. Optimality of an alignment. EDAN40: Functional Programming Assignment 2: String alignment

Parallel Haskell on MultiCores and Clusters

CS 320 Midterm Exam. Fall 2018

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

Haskell & functional programming, some slightly more advanced stuff. Matteo Pradella

A tour of the Haskell Prelude

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

An introduction to functional programming. July 23, 2010

CSci 450: Org. of Programming Languages Overloading and Type Classes

Introduction to Programming: Lecture 6

Haskell An Introduction

Course year Typeclasses and their instances

Week 5 Tutorial Structural Induction

Functional Programming. Overview. Topics. Recall λ-terms. Examples

10/21/08. Monads for functional programming uses! Kathleen Fisher!

Functional Programming

Chapter 15. Functional Programming Languages

Lecture 4: Higher Order Functions

Principles of Programming Languages (H)

Witnessing Purity, Constancy and Mutability APLAS Ben Lippmeier Australian National University 2009/12/14

Abstract Types, Algebraic Types, and Type Classes

Programming Languages 3. Definition and Proof by Induction

Haskell: yet another Functional Language. Distributed and Parallel Technology

Streams and Evalutation Strategies

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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.

Haskell Types COMP360

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Haskell Types COMP360

CSCE 314 Programming Languages

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

Functional Programming

Introduction to Functional Programming in Haskell 1 / 56

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

CS 11 Haskell track: lecture 1

CSE Programming Languages Midterm - Winter 2010

Transcription:

An Introduction to Programming in Haskell Gabriel J. Ferrer Hendrix College

Overview Functional programming Property testing Iteration Type system I/O

Installation Go to: http://www.haskell.org/ Download the Haskell Platform Windows, Mac, Linux are all available Interactive interpreter runs in web browser

Functional Programming Computations are mathematical expressions Functions are first-class values Referential Transparency

A First Haskell Function fact :: Integer -> Integer fact n n == 0 = 1 otherwise = n * fact (n - 1)

A Calculation numitems :: [a] -> Integer numitems [] = 0 numitems (x:xs) = 1 + numitems xs

A Transformation timesn :: [Int] -> Int -> [Int] timesn [] _ = [] timesn (x:xs) n = (x * n):(timesn xs n)

Filtering positive :: [Int] -> [Int] positive [] = [] positive (x:xs) x > 0 = x:(positive xs) otherwise = positive xs

Write these functions: addup :: [Int] -> Int negateall :: [Int] -> [Int] alleven :: [Int] -> Bool keepevens :: [Int] -> [Int]

Property Testing Write a formal specification in Haskell Generate random tests of the specification import Test.QuickCheck

Some Properties prop_doublenegate :: [Int] -> Bool prop_doublenegate l = negateall (negateall l) == l prop_checkevens :: [Int] -> Bool prop_checkevens l = alleven $ keepevens l prop_subaddup :: [Int] -> Bool prop_subaddup l = addup l + addup (negateall l) == 0

Iteration Functions are first-class values Define general iteration structures taking functions as arguments

Examples Revisited fact :: Integer -> Integer fact n = foldr (*) 1 [2..n]

Examples Revisited numitems :: [a] -> Integer numitems l = foldr inc 0 l where inc _ total = total + 1

Examples Revisited timesn :: [Int] -> Int -> [Int] timesn l n = map (n*) l

Examples Revisited positive :: [Int] -> [Int] positive l = filter (\n -> n > 0) l

What is foldr? foldr :: (a -> b -> b) -> b -> [a] -> b foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs)

What is map? map :: (a -> b) -> [a] -> [b] map f l = foldr step [] l where step x xs = (f x):xs

What is filter? filter :: (a -> Bool) -> [a] -> [a] filter p l = foldr step [] l where step x xs p x = x:xs otherwise = xs

Using higher-order functions: addup :: [Int] -> Int negateall :: [Int] -> [Int] alleven :: [Int] -> Bool keepevens :: [Int] -> [Int]

List Comprehensions timesn :: [Int] -> Int -> [Int] timesn l n = [x * n x <- l] positive :: [Int] -> [Int] positive l = [x x <- l, x > 0]

A Nested Loop points :: Int -> Int -> [(Int,Int)] points w h = [(x,y) x <- [1..w], y <- [1..h]]

Using list comprehensions: negateall :: [Int] -> [Int] keepevens :: [Int] -> [Int] halves :: [Int] -> [Int] halfevens :: [Int] -> [Int]

More about lists Infinite lists [1..] [1,3..] [2,4..] take, drop

Customized Infinite Lists iterate (1+) 0 iterate (2*) 1

Using Infinite Lists facts :: [Integer] facts = 1:[n*f (f,n) <- zip facts [1..]] facts!! 5

Fibonacci Numbers fibs :: [Integer] fibs = 0 : 1 : [x+y (x,y)<- zip fibs $ tail fibs]

zipwith facts :: [Integer] facts = 1:zipWith (*) facts [1..] fibs :: [Integer] fibs = 0:1:zipWith (+) fibs (tail fibs)

Data Types data Coin = Heads Tails toss :: Coin -> Coin toss Heads = Tails toss Tails = Heads

Type Classes class Eq a where (==), (/=) :: a -> a -> Bool x /= y = not (x == y) instance Eq Coin where Heads == Heads = True Tails == Tails = True Heads == Tails = False Tails == Heads = False

deriving data Coin = Heads Tails deriving (Eq, Show)

Write these functions: tossn :: Int -> Coin -> [Coin] numof :: (Eq a) => a -> [a] -> Int -- Example: numh :: Int -> Int numh n = numof Heads $ tossn n Heads

Binary Tree data Tree a = Node a (Tree a) (Tree a) Sentinel deriving (Eq, Show)

The Ord class class (Eq a) => Ord a where (<),(<=),(>=),(>) :: a -> a -> Bool max, min :: a -> a -> a

Tree Exercise empty :: Tree a -> Bool add :: (Ord a) => a -> Tree a -> Tree a has :: (Ord a) => Tree a -> a -> Bool

Arrays import Data.Array fibarray n = a where a = array (0,n) ([(0, 1), (1, 1)] ++ [(i, a!(i-2) + a!(i-1)) i <- [2..n]])

Monads Side effects disrupt referential transparency Fatal for lazy evaluation Solution: Package up totally ordered side effects

IO Monad main :: IO () main = do x <- getline putstrln x

Random Numbers import System.Random main = do gen <- newstdgen let ns = randoms gen :: [Int] print $ take 10 ns

Mutable Arrays import Control.Monad import Control.Monad.ST import Data.Array.ST fib8 n = runst $ do arr <- newarray (0, n) 0 :: ST s (STArray s Integer Integer) writearray arr 0 0 writearray arr 1 1 form_ [2..n] $ \i -> do val1 <- readarray arr (i - 2) val2 <- readarray arr (i - 1) writearray arr i $ val1 + val2 result <- readarray arr n return result

Resources www.haskell.org hackage.haskell.org Akin to CPAN in the Perl world http://book.realworldhaskell.org/read/ http://ozark.hendrix.edu/~ferrer/ presentations/