Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.
|
|
- Phoebe Cunningham
- 5 years ago
- Views:
Transcription
1 Practical Practical An introduction to functional programming July 21, 2011
2 Contents Practical
3 Practical is fun, and that s what it s all about! Even if seems strange to you at first, don t give up. Learning is almost like learning to program for the first time all over again. It s fun, and it forces you to think differently. Miran Lipovača
4 is a purely functional programming language. You tell the computer what stuff is. The factorial of a number is the product of every integer from 1 to that number. This operation can be expressed as a function: ghci> let factorial n = product [1..n] You can t set a variable to one value and then set it to something else later on. A function has no side effects. The only thing a function can do is calculate something and return the result. Referential transparency: If a function is called twice with the same parameters, it s guaranteed to return the same result both times. Practical
5 Practical has strong types. ghci> let x = 1 :: Int ghci> let y = 1 :: Double ghci> x + y Couldn t match expected type... has static types. ghci> True && "False" Couldn t match expected type... s combination of strong and static typing makes it impossible for type errors to occur at runtime.
6 Practical has type inference. ghci> let factorial n = product [1..n] ghci> :type factorial factorial :: (Num t, Enum t) => t -> t is lazy. ghci> True length [1..] > 0 True is elegant and concise. qsort :: (Ord a) => [a] -> [a] qsort [] = [] qsort (x:xs) = qsort (filter (<= x) xs) ++ [x] ++ qsort (filter (> x) xs) is novel, powerful, and fun.
7 GHC (The Glasgow Compiler) Practical The Glasgow Compiler is a state-of-the-art, open source, compiler and interactive environment for the functional language. GHC has two main components: GHCi (ghci) is an interactive interpreter and debugger. GHC (ghc) is an optimizing compiler. The Glasgow Compiler (GHC).
8 The Platform Practical : Batteries included. The Platform is a comprehensive, robust development environment for programming in. The platform makes it trivial to get up and running with a full development environment. Comprehensive, robust, and cutting edge. Current stable release: (April 2011) The Platform. hackage.haskell.org/platform
9 GNU Emacs Practical GNU Emacs is an extensible, customizable text editor, and more. The current stable release is GNU Emacs. mode is a major Emacs mode for editing source code. mode. projects.haskell.org/haskellmode-emacs
10 The community Practical The Communities and Activities Report. in industry. in education. in research. and mathematics. Planet, The Monad.Reader
11 Frag Practical
12 Monadius Practical
13 Agda, Darcs, and xmonad Practical Agda is a dependently typed functional programming language and a proof assistant. wiki.portal.chalmers.se/agda Darcs is a free, open source code management system. darcs.net xmonad is a dynamically tiling X11 window manager that is written and configured in. xmonad.org
14 xmonad Practical
15 Practical
16 Example () 1. $ ghci Prelude> "" "" 2. Prelude> putstr "\n" Prelude> :quit 3. $ cat hello.hs main :: IO () main = putstrln "" $ ghc hello.hs $./a.out $ ghci Practical
17 Example (Factorial) factorial :: (Integral a) => a -> a 1. factorial n = if n == 0 then 1 else n * factorial (n - 1) 2. factorial 0 = 1 factorial n = n * factorial (n - 1) 3. factorial n n == 0 = 1 otherwise = n * factorial (n - 1) 4. Prelude> let factorial n = product [1..n] Prelude> factorial Practical
18 Practical Prelude> :set prompt "ghci> " Examples (Basic arithmetic) ghci> ghci> 11 * 6 66 ghci> (-) ghci> 9 /
19 Examples (The order of operations) ghci> (34 * 88) ghci> 34 * ghci> 34 * ( ) Practical Examples (Negative numbers) ghci> 9 * -8 Precedence parsing error ghci> 9 * (-8) -72
20 Examples (Boolean algebra) ghci> True && False False ghci> False True True ghci> not (False && True) True Practical Examples (Equality and inequality) ghci> 5 == 5 True ghci> "hello" /= "hello" False
21 ghci> :set +t Examples (Mathematical constants) ghci> pi it :: Double ghci> e... Not in scope: e ghci> exp it :: Double Practical ghci> :unset +t
22 Examples (Functions) ghci> succ ghci> min max ghci> succ 2 * ghci> succ (2 * 10) 21 ghci> div ghci> 10 div 2 5 Practical
23 Practical
24 Types Practical ghci> :t f f :: Char ghci> :t "foo" "foo" :: [Char] Common types Int Integers 1, 2, 3, 4,... Integer Integers 1, 2, 3, 4,... Float Floating point numbers 1.0, 2.0, 3.0, 4.0,... Double Floating point numbers 1.0, 2.0, 3.0, 4.0,... Bool Booleans True, False Char Unicode characters a, b, c, d,... In, every expression and function has a type.
25 Lists Practical Examples ghci> ["David","Roger"] ["David","Roger"] ghci> [True,False,"True"] Couldn t match expected type... ghci> "Hello" ++ ", " ++ "world!" "" ghci> [ h, e ] ++ [ l, l, o ] "hello" ghci> [1..5] ++ [5,4..1] [1,2,3,4,5,5,4,3,2,1]
26 Lists Practical Examples ghci> a :[ b.. z ] "abcdefghijklmnopqrstuvwxyz" ghci> 1:2:3:4:5:[] [1,2,3,4,5] ghci> [ a.. y ]: z Couldn t match expected type... ghci> [[1..5],[5,4..1]] [[1,2,3,4,5],[5,4,3,2,1]]
27 Lists Examples ghci> [3,4,2] < [3,4,3] True ghci> [3,4,2] < [2,4] False ghci> [3,2,1] > [2,10,100] True ghci> "gilmour" > "waters" False ghci> [3,4,2] == [3,4,2] True ghci> [[],[]] == [[],[],[]] False Practical
28 Lists Practical Examples (List operations) ghci> head [1..5] ghci> last [1..5] 1 5 ghci> init [1..5] ghci> tail [1..5] [1,2,3,4] [2,3,4,5] ghci> null [] ghci> null [[]] True False ghci> take 3 [1..5] ghci> take 100 [1..5] [1,2,3] [1,2,3,4,5] ghci> drop 3 [1..5] ghci> drop 100 [1..5] [4,5] []
29 Lists Practical Examples (List operations) ghci> maximum [1,11,22] 22 ghci> minimum [1,11,22] 1 ghci> sum [1..10] 55 ghci> product [10,9..1] ghci> a elem "hello" False
30 Lists Practical Examples (Ranges) ghci> [ a.. z ] "abcdefghijklmnopqrstuvwxyz" ghci> [7,14..7*10] [7,14,21,28,35,42,49,56,63,70] ghci> [70,63..7] [70,63,56,49,42,35,28,21,14,7] ghci> [0.5, ] [0.5,0.7, , ]
31 Lists Practical Examples (List comprehensions) ghci> [x * 2 x <- [ ], x mod 7 == 0] [112,126,140,154,168,182,196] pairs :: [a] -> [b] -> [(a,b)] pairs xs ys = [(x,y) x <- xs, y <- ys] perms :: (Eq a) => [a] -> [[a]] perms [] = [[]] perms xs = [x:ps x <- xs, ps <- perms (xs \\ [x])]
32 Tuples Practical Examples ghci> :t (True,"True", t ) (True,"True", t ) :: (Bool, [Char], Char) ghci> fst (1,2) 1 ghci> snd (1,2) 2 ghci> :t zip zip :: [a] -> [b] -> [(a, b)] ghci> zip [1..] [ a.. d ] [(1, a ),(2, b ),(3, c ),(4, d )]
33 Example (linecount) $ cat lc.hs main :: IO () main = interact linecount where linecount :: String -> String linecount input = show (length (lines input)) ++ "\n" $ runhaskell lc.hs < lc.hs 6 $ runhaskell lc.hs < hello.hs 2 Practical
34 Algebraic types Practical Algebraic data type definitions are introduced by the keyword data, followed by the name of the type, an equals sign and then the constructors of the type being defined. The simplest sort of algebraic type is defined by enumerating the elements of the type. Examples 1. data Temp = Cold Hot 2. data Season = Spring Summer data Bool = True False
35 Algebraic types Practical Examples 1. type Name = String type Age = Int data Person = Person Name Age 2. data Shape = Circle Float Rectangle Float Float deriving (Eq,Ord,Show,Read)
36 Pattern matching Practical Pattern matching is used to specify patterns to which some data should conform and to deconstruct the data according to those patterns. Example (sayme) sayme :: Int -> String sayme 1 = "One!" sayme 2 = "Two!" sayme 3 = "Three!" sayme 4 = "Four!" sayme 5 = "Five!" sayme x = "Not between 1 and 5"
37 Pattern matching Practical Example (length) length returns the length of a finite list. length :: [a] -> Int length [] = 0 length (_:xs) = 1 + length xs ghci> length [] 0 ghci> length [1..5] 5
38 Pattern matching Example (Numerical expressions) data Expr = Lit Int Add Expr Expr Sub Expr Expr eval :: Expr -> Int eval (Lit n) = n eval (Add e1 e2) = (eval e1) + (eval e2) eval (Sub e1 e2) = (eval e1) - (eval e2) Practical : Define a function eqexpr :: Expr -> Expr -> Bool that compares two numerical expressions for equality.
39 Guards Practical Patterns are used to check if the values passed to a function are constructed in a certain way. Guards are used to check if some property of those passed values is true or false. Example (sayme) sayme :: Int -> String sayme n n == 1 = "One!" n == 2 = "Two!" n == 3 = "Three!" n == 4 = "Four!" otherwise = "Not between 1 and 5"
40 Lazy evaluation Practical will compute only what it really must. Examples (Infinite lists) 1. ones :: [Int] ones = 1 : ones 2. powers :: Int -> [Int] powers n = [n^x x <- [0..]] 3. pythagtriples :: [(Int,Int,Int)] pythagtriples = [(x,y,z) z <- [2..], y <- [2..z - 1], x <- [2..y - 1], x*x + y*y == z*z]
41 Lazy evaluation Example (The sieve of Eratosthenes) primes :: [Integer] primes = sieve [2..] sieve :: [Integer] -> [Integer] sieve (x:xs) = x : sieve [y y <- xs, y mod x > 0] Practical memberord :: (Ord a) => [a] -> a -> Bool memberord (x:xs) n x < n = memberord xs n x == n = True otherwise = False
42 Practical
43 Polymorphism A value is polymorphic if, depending on the context where it is used, it can take more than one type. Example (last) ghci> :type last last :: [a] -> a ghci> last [1,9,8,9] 9 ghci> last "Ummagumma" a Practical Parametric polymorphism is when a function s type signature allows various arguments to take on arbitrary types, but the types must be related to each other in some way.
44 Polymorphism Ad hoc polymorphism (or overloading) is when the possible types are limited and must be individually specified before use. Example (show) ghci> :type show show :: (Show a) => a -> String ghci> show 22 "22" ghci> show 2.2 "2.2" Practical In, ad hoc polymorphism is achieved by type classes.
45 Recursion Practical Recursion is a way of defining functions in which a function calls itself. Example (reverse) reverse returns the elements of a list in reverse order. reverse :: [a] -> [a] reverse [] = [] reverse (x:xs) = reverse xs ++ [x] ghci> reverse [1,2,3,4,5] [5,4,3,2,1] ghci> reverse "man o nam" "man o nam"
46 Recursion Practical Example (repeat) repeat takes an element and returns an infinite list composed of that element. repeat :: a -> [a] repeat x = x : repeat x ghci> let ones = repeat 1 ghci> take 5 ones [1,1,1,1,1] ghci> replicate 5 1 [1,1,1,1,1]
47 Recursion Practical Example (zip) zip takes two lists and zips them together. zip :: [a] -> [b] -> [(a,b)] zip (x:xs) (y:ys) = (x,y) : zip xs ys zip = [] ghci> zip [1,2,3] ["one","two"] [(1,"one"),(2,"two")]
48 Recursion Practical Example (quicksort) quicksort :: (Ord a) => [a] -> [a] quicksort [] = [] quicksort (x:xs) = let left = [a a <- xs, a <= x] right = [a a <- xs, a > x] in quicksort left ++ [x] ++ quicksort right ghci> quicksort ("the quick brown fox " ++ "jumps over the lazy dog") " abcdeeefghhijklmnoooopqrrsttuuvwxyz"
49 Higher-order functions Practical functions can take functions as parameters and return functions as return values. Curried functions and partial application In, all functions take only one argument. ghci> max 4 5 ghci> (max 4) ghci> let max4 = max 4 ghci> max4 5 5 max :: (Ord a) => a -> a -> a can also be written as max :: (Ord a) => a -> (a -> a)
50 Higher-order functions Practical Example (zipwith) zipwith generalizes zip by zipping with the function given as the first argument. zipwith :: (a b c) [a] [b] [c] zipwith f (x:xs) (y:ys) = f x y : zipwith f xs ys zipwith _ = [] ghci> zipwith (+) [1,2,3,4,5] [1,2,3,2,1] [2,4,6,6,6] ghci> zipwith (*) (replicate 5 2) [1..] [2,4,6,8,10]
51 Higher-order functions Practical Example (flip) flip takes a function and returns a function like the original, but with the first two arguments flipped. flip :: (a -> b -> c) -> b -> a -> c flip f y x = f x y ghci> zip [1..5] "aeiou" [(1, a ),(2, e ),(3, i ),(4, o ),(5, u )] ghci> flip zip [1..5] "aeiou" [( a,1),( e,2),( i,3),( o,4),( u,5)]
52 Higher-order functions Practical Example (map) map f xs is the list obtained by applying f to each element of xs. map :: (a -> b) -> [a] -> [b] map _ [] = [] map f (x:xs) = f x : map f xs ghci> map (+ 2) [1,2,3,2,1] [3,4,5,4,3] ghci> map even [1..5] [False,True,False,True,False]
53 Higher-order functions Example (filter) filter takes a predicate and a list, and returns the list of elements that satisfy the predicate. filter :: (a -> Bool) -> [a] -> [a] filter _ [] = [] filter p (x:xs) p x = x : filter p xs otherwise = filter p xs ghci> filter (> 2) [1,2,3,2,1] [3] ghci> filter even [1..5] [2,4] Practical
54 Higher-order functions Practical Example (quicksort) quicksort :: (Ord a) => [a] -> [a] quicksort [] = [] quicksort (x:xs) = quicksort left ++ [x] ++ quicksort right where left = filter (<= x) xs right = filter (> x) xs
55 Higher-order functions Lambdas Lambdas are anonymous functions that are used when a function is needed only once. ghci> map (\x -> x + 2) [1,2,3,2,1] [3,4,5,4,3] Example (flip) Practical flip :: (a -> b -> c) -> b -> a -> c flip f = \x y -> f y x ghci> zip [1..5] "aeiou" [(1, a ),(2, e ),(3, i ),(4, o ),(5, u )] ghci> flip zip [1,2,3,4,5] "aeiou" [( a,1),( e,2),( i,3),( o,4),( u,5)]
56 Higher-order functions Examples (Application operator) ($) :: (a -> b) -> a -> b f $ x = f x ghci> sum $ filter (> 10) $ map (* 2) [2..10] 80 ghci> map ($ 3) [(4 +),(10 *),(^ 2),sqrt] [7.0,30.0,9.0, ] Practical Examples (Function composition) (.) :: (b -> c) -> (a -> b) -> a -> c f. g = \x -> f (g x) ghci> map (negate. abs) [1,2,-3,-5,4] [-1,-2,-3,-5,-4]
57 Practical 1. Understanding the problem. 2. Designing the program. 3. Writing the program. 4. Looking back. Simon Thompson. Where do I begin? A problem solving approach in teaching functional programming
58 Example (The maximum of three integers) 1. Understanding the problem. maxthree :: Int -> Int -> Int -> Int 2. Designing and writing the program. maxtwo :: Int -> Int -> Int maxtwo a b a <= b = b otherwise = a maxthree a b c b <= a && c <= a = a a <= b && c <= b = b otherwise = c maxthree a b c = maxtwo (maxtwo a b) c 3. Looking back. Practical
59 Learn you a for great good! Practical
60 Learn you a for great good! Practical 1. Read chapters 1-6 of the tutorial Learn you a for great good! 2. Answer the following questions. What do you like/dislike about functional programming? What do you like/dislike about? What do you think of the tutorial?... Miran Lipovača. Learn you a for great good! No Starch Press, learnyouahaskell.com
61 Practical
62 Practical Miran Lipovača. Learn you a for great good! No Starch Press, learnyouahaskell.com Bryan O Sullivan, John Goerzen, and Don Stewart. Real world. O Reilly, book.realworldhaskell.org Simon Thompson. : The craft of functional programming. Addison-Wesley,
Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza
Haskell 101 (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza Haskell 101: Contents Introduction Tutorial Homework Bibliography Haskell 101: Contents Introduction Tutorial Homework Bibliography Haskell
More informationAn introduction to functional programming. July 23, 2010
An introduction to functional programming July 23, 2010 About Outline About About What is functional programming? What is? Why functional programming? Why? is novel. is powerful. is fun. About A brief
More informationHaskell An Introduction
Haskell An Introduction What is Haskell? General purpose Purely functional No function can have side-effects IO is done using special types Lazy Strongly typed Polymorphic types Concise and elegant A First
More informationProgramming Languages Fall 2013
Programming Languages Fall 2013 Lecture 2: types Prof. Liang Huang huang@qc.cs.cuny.edu Recap of Lecture 1 functional programming vs. imperative programming basic Haskell syntax function definition lazy
More informationHaske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell
Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dk The most popular purely functional, lazy programming language Functional programming language : a program
More informationCS 11 Haskell track: lecture 1
CS 11 Haskell track: lecture 1 This week: Introduction/motivation/pep talk Basics of Haskell Prerequisite Knowledge of basic functional programming e.g. Scheme, Ocaml, Erlang CS 1, CS 4 "permission of
More informationAn introduction introduction to functional functional programming programming using usin Haskell
An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dkau Haskell The most popular p purely functional, lazy programming g language Functional programming language : a program
More informationProgramming Paradigms
PP 2017/18 Unit 11 Functional Programming with Haskell 1/37 Programming Paradigms Unit 11 Functional Programming with Haskell J. Gamper Free University of Bozen-Bolzano Faculty of Computer Science IDSE
More informationA general introduction to Functional Programming using Haskell
A general introduction to Functional Programming using Haskell Matteo Rossi Dipartimento di Elettronica e Informazione Politecnico di Milano rossi@elet.polimi.it 1 Functional programming in a nutshell
More informationFunctional Programming in Haskell Part I : Basics
Functional Programming in Haskell Part I : Basics Madhavan Mukund Chennai Mathematical Institute 92 G N Chetty Rd, Chennai 600 017, India madhavan@cmi.ac.in http://www.cmi.ac.in/ madhavan Madras Christian
More informationINTRODUCTION TO FUNCTIONAL PROGRAMMING
INTRODUCTION TO FUNCTIONAL PROGRAMMING Graham Hutton University of Nottingham adapted by Gordon Uszkay 1 What is Functional Programming? Opinions differ, and it is difficult to give a precise definition,
More informationAccording to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!
Lecture 7: Haskell CSC 131 Fall, 2014 Kim Bruce According to Larry Wall (designer of PERL): a language by geniuses for geniuses He s wrong at least about the latter part though you might agree when we
More informationPROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions
PROGRAMMING IN HASKELL CS-205 - Chapter 6 - Recursive Functions 0 Introduction As we have seen, many functions can naturally be defined in terms of other functions. factorial :: Int Int factorial n product
More informationIt is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis
Chapter 14 Functional Programming Programming Languages 2nd edition Tucker and Noonan It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis
More informationHaskell through HUGS THE BASICS
Haskell through HUGS THE BASICS FP for DB Basic HUGS 1 Algorithmic Imperative Languages variables assignment if condition then action1 else action2 loop block while condition do action repeat action until
More informationStandard prelude. Appendix A. A.1 Classes
Appendix A Standard prelude In this appendix we present some of the most commonly used definitions from the standard prelude. For clarity, a number of the definitions have been simplified or modified from
More informationCSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.
CSC312 Principles of Programming Languages : Functional Programming Language Overview of Functional Languages They emerged in the 1960 s with Lisp Functional programming mirrors mathematical functions:
More informationHaskell Types, Classes, and Functions, Currying, and Polymorphism
1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Types, Classes, and Functions, Currying, and Polymorphism 2 Types A type is a collection of related values. For example, Bool contains the
More informationCS 360: Programming Languages Lecture 10: Introduction to Haskell
CS 360: Programming Languages Lecture 10: Introduction to Haskell Geoffrey Mainland Drexel University Thursday, February 5, 2015 Adapted from Brent Yorgey s course Introduction to Haskell. Section 1 Administrivia
More informationINTRODUCTION TO HASKELL
INTRODUCTION TO HASKELL PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/81 HASKELL: A PURELY FUNCTIONAL PROGRAMMING LANGUAGE Functions are first-class values: Can be
More informationIntroduction to Haskell
Introduction to Haskell Matt Mullins Texas A&M Computing Society October 6, 2009 Matt Mullins (TACS) Introduction to Haskell October 6, 2009 1 / 39 Outline Introduction to Haskell Functional Programming
More informationAbstract Types, Algebraic Types, and Type Classes
Informatics 1 Functional Programming Lectures 13 and 14 Monday 9 and Tuesday 10 November 2009 Abstract Types, Algebraic Types, and Type Classes Philip Wadler University of Edinburgh Reminders Tutorial
More informationShell CSCE 314 TAMU. Functions continued
1 CSCE 314: Programming Languages Dr. Dylan Shell Functions continued 2 Outline Defining Functions List Comprehensions Recursion 3 A Function without Recursion Many functions can naturally be defined in
More informationIntroduction to Functional Programming in Haskell 1 / 56
Introduction to Functional Programming in Haskell 1 / 56 Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order
More informationCS 320: Concepts of Programming Languages
CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 08: Type Classes o o Review: What is a type class? Basic Type Classes: Eq, Ord, Enum, Integral,
More informationA tour of the Haskell Prelude
A tour of the Haskell Prelude Bernie Pope 2001 1 Haskell The Haskell language was conceived during a meeting held at the 1987 Functional Programming and Computer Architecture conference (FPCA 87). At the
More informationFunctional Programming and Haskell
Functional Programming and Haskell Tim Dawborn University of Sydney, Australia School of Information Technologies Tim Dawborn Functional Programming and Haskell 1/22 What are Programming Paradigms? A programming
More informationPROGRAMMING IN HASKELL. Chapter 2 - First Steps
PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 The Hugs System Hugs is an implementation of Haskell 98, and is the most widely used Haskell system; The interactive nature of Hugs makes it well suited
More informationn n Try tutorial on front page to get started! n spring13/ n Stack Overflow!
Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!
More informationFirst Haskell Exercises
First Haskell Exercises CS110 November 23, 2016 Although it is possible to install Haskell on your computer, we want to get started quickly, so we will be using an online Haskell programming system (also
More informationAdvanced Topics in Programming Languages Lecture 2 - Introduction to Haskell
Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell Ori Bar El Maxim Finkel 01/11/17 1 History Haskell is a lazy, committee designed, pure functional programming language that
More informationCSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona
1/43 CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg Functions over Lists
More informationHaskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules
Haskell: From Basic to Advanced Part 2 Type Classes, Laziness, IO, Modules Qualified types In the types schemes we have seen, the type variables were universally quantified, e.g. ++ :: [a] -> [a] -> [a]
More informationCS 360: Programming Languages Lecture 12: More Haskell
CS 360: Programming Languages Lecture 12: More Haskell Geoffrey Mainland Drexel University Adapted from Brent Yorgey s course Introduction to Haskell. Section 1 Administrivia Administrivia Homework 5 due
More informationLogic - CM0845 Introduction to Haskell
Logic - CM0845 Introduction to Haskell Diego Alejandro Montoya-Zapata EAFIT University Semester 2016-1 Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester
More informationPROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions
PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions 0 Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x 2 x {1...5}} The set {1,4,9,16,25}
More informationHaskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder
Outline 1 2 3 4 What is Haskell? Haskell is a functional programming language. Characteristics functional non-strict ( lazy ) pure (no side effects*) strongly statically typed available compiled and interpreted
More informationInformatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November Type Classes. Don Sannella University of Edinburgh
Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November 2013 Type Classes Don Sannella University of Edinburgh Mock exam Slots and rooms have now been assigned Mon 18
More informationHIGHER-ORDER FUNCTIONS
5 HIGHER-ORDER FUNCTIONS Haskell functions can take functions as parameters and return functions as return values. A function that does either of these things is called a higher-order function. Higher-order
More informationLecture 19: Functions, Types and Data Structures in Haskell
The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming
More informationFunctional Programming for Logicians - Lecture 1
Functional Programming for Logicians - Lecture 1 Functions, Lists, Types Malvin Gattinger 4 June 2018 module L1 where Introduction Who is who Course website: https://malv.in/2018/funcproglog/ Malvin Gattinger
More informationHigher Order Functions in Haskell
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
More informationCourse year Typeclasses and their instances
Course year 2016-2017 Typeclasses and their instances Doaitse Swierstra and Atze Dijkstra with extra s Utrecht University September 29, 2016 1. The basics 2 Overloading versus parametric polymorphism 1
More informationAdvanced features of Functional Programming (Haskell)
Advanced features of Functional Programming (Haskell) Polymorphism and overloading January 10, 2017 Monomorphic and polymorphic types A (data) type specifies a set of values. Examples: Bool: the type of
More informationPrinciples of Programming Languages
Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Introduc;on to Hakell Lesson 27! 1 The origins: ML programming
More informationLittle and not so big bits of Haskell
Little and not so big bits of Haskell D. Renault Haskell (small) School LaBRI October 2015, v. 1.2.1 D. Renault (LaBRI) Little and not so big bits of Haskell October 2015, v. 1.2.1 1 / 45 1990 - A committee
More informationIntro to Haskell Notes: Part 5
Intro to Haskell Notes: Part 5 Adrian Brasoveanu October 5, 2013 Contents 1 Curried functions and related issues 1 1.1 Curried functions......................................... 1 1.2 Partially applied
More informationCSc 372 Comparative Programming Languages
CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Christian Collberg collberg+372@gmail.com Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg
More informationFunctional Programming in Haskell for A level teachers
Functional Programming in Haskell for A level teachers About this document Functional Programming is now part of the A level curriculum. This document is intended to get those who already have some programming
More information(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.
EXAM FUNCTIONAL PROGRAMMING Tuesday the 1st of October 2016, 08.30 h. - 10.30 h. Name: Student number: Before you begin: Do not forget to write down your name and student number above. If necessary, explain
More informationSolution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors
Solution sheet 1 Introduction Please note that there can be other solutions than those listed in this document. This is a literate Haskell file which is available as PDF, as well as literate Haskell source
More informationCSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics
1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Basics 2 Contents 1. Jump into Haskell: Using ghc and ghci (more detail) 2. Historical Background of Haskell 3. Lazy, Pure, and Functional
More informationFUNCTIONAL PROGRAMMING 1 HASKELL BASICS
1 FUNCTIONAL PROGRAMMING 1 HASKELL BASICS Dr. Ahmed Sallam Reference 2 Main: Introduction to Haskell Further By Brent Yorgey Functional Programming 3 Function is the atom of the language, and can be used
More information3. Functional Programming. Oscar Nierstrasz
3. Functional Programming Oscar Nierstrasz Roadmap > Functional vs. Imperative Programming > Pattern Matching > Referential Transparency > Lazy Evaluation > Recursion > Higher Order and Curried Functions
More informationCOP4020 Programming Languages. Functional Programming Prof. Robert van Engelen
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts
More informationParallel Haskell on MultiCores and Clusters
Parallel Haskell on MultiCores and Clusters (part of Advanced Development Techniques) Hans-Wolfgang Loidl School of Mathematical and Computer Sciences Heriot-Watt University, Edinburgh (presented at the
More informationTitle: Recursion and Higher Order Functions
Programing Paradigms and Languages Title: Recursion and Higher Order Functions Students: Ysee Monir, Besart Vishesella Proffesor: Dr. Robert Kowalczyk In this presentation, we'll take a closer look at
More informationCSCE 314 Programming Languages
CSCE 314 Programming Languages Final Review Part I Dr. Hyunyoung Lee 1 Programming Language Characteristics Different approaches to describe computations, to instruct computing devices E.g., Imperative,
More informationCSci 450: Org. of Programming Languages Overloading and Type Classes
CSci 450: Org. of Programming Languages Overloading and Type Classes H. Conrad Cunningham 27 October 2017 (after class) Contents 9 Overloading and Type Classes 1 9.1 Chapter Introduction.........................
More informationIntroduction. chapter Functions
chapter 1 Introduction In this chapter we set the stage for the rest of the book. We start by reviewing the notion of a function, then introduce the concept of functional programming, summarise the main
More informationCSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions
1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions
More informationHaskell: From Basic to Advanced. Haskell buzzwords. Hello, World! History. Haskell 98 / Haskell 2010 GHC
Haskell: From Basic to Advanced Haskell buzzwords Part 1 Basic Language Functional Pure Lazy Strong static typing Type polymorphism Type classes Monads Haskell 98 / Haskell 2010 GHC Glasgow Haskell Compiler
More informationReferences. Monadic I/O in Haskell. Digression, continued. Digression: Creating stand-alone Haskell Programs
References Monadic I/O in Haskell Jim Royer CIS 352 March 6, 2018 Chapter 18 of Haskell: the Craft of Functional Programming by Simon Thompson, Addison-Wesley, 2011. Chapter 9 of Learn you a Haskell for
More informationProgramming Paradigms and Languages Introduction to Haskell. dr Robert Kowalczyk WMiI UŁ
Programming Paradigms and Languages Introduction to Haskell dr Robert Kowalczyk WMiI UŁ Functional programming In functional programming (special type of declarative programming), programs are executed
More informationHaskell Overview II (2A) Young Won Lim 8/9/16
(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
More informationAlgebraic Types. Chapter 14 of Thompson
Algebraic Types Chapter 14 of Thompson Types so far Base types Int, Integer, Float, Bool, Char Composite types: tuples (t 1,t 2,,t n ) lists [t 1 ] functions (t 1 -> t 2 ) Algebraic types enumerated, product
More informationBackground Operators (1E) Young Won Lim 7/7/18
Background Operators (1E) Copyright (c) 2016-2018 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
More informationBackground Type Classes (1B) Young Won Lim 6/28/18
Background Type Classes (1B) Copyright (c) 2016-2017 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
More informationEDAF40. 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.
EDAF40 2nd June 2017 14: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. DO NOT WRITE WITH OTHER COLOUR THAN BLACK - coloured text
More informationCSCE 314 Programming Languages
CSCE 314 Programming Languages Haskell 101 Dr. Hyunyoung Lee 1 Contents 1. Historical Background of Haskell 2. Lazy, Pure, and Functional Language 3. Using ghc and ghci 4. Functions 5. Haskell Scripts
More informationFunctional Programming in Haskell Part 2 : Abstract dataypes and infinite structures
Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures Madhavan Mukund Chennai Mathematical Institute 92 G N Chetty Rd, Chennai 600 017, India madhavan@cmi.ac.in http://www.cmi.ac.in/
More informationProgramming Languages Fall Prof. Liang Huang
Programming Languages Fall 2014 Prof. Liang Huang huang@qc.cs.cuny.edu Computer Science is no more about computers than astronomy is about telescopes. (Mis)attributed to Edsger Dijkstra, 1970. Computer
More informationChapter 15. Functional Programming Languages
Chapter 15 Functional Programming Languages Copyright 2009 Addison-Wesley. All rights reserved. 1-2 Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages
More informationFunctional Programming I *** Functional Programming and Interactive Theorem Proving. Ulrich Berger Michaelmas Term 2006
Functional Programming I *** Functional Programming and Interactive Theorem Proving Ulrich Berger Michaelmas Term 2006 2 *** Room 306 (Faraday Tower) Phone 513380 Fax 295708 u.berger@swansea.ac.uk http://www-compsci.swan.ac.uk/
More information301AA - Advanced Programming [AP-2017]
301AA - Advanced Programming [AP-2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP-2017-15: Func'onal
More informationTesting. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]
Testing Advanced functional programming - Lecture 2 Wouter Swierstra and Alejandro Serrano 1 Program Correctness 2 Testing and correctness When is a program correct? 3 Testing and correctness When is a
More informationCSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona
1/40 CSc 372 Comparative Programming Languages 4 : Haskell Basics Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/40 The Hugs Interpreter The
More informationIntro to Haskell Notes: Part 1
Intro to Haskell Notes: Part 1 Adrian Brasoveanu July 1, 2013 Contents 1 Comments 2 2 Simple arithmetic 2 3 Boolean operators 2 4 Testing for equality 3 5 No covert type conversion 3 6 Functions 4 6.1
More informationImperative languages
Imperative languages Von Neumann model: store with addressable locations machine code: effect achieved by changing contents of store locations instructions executed in sequence, flow of control altered
More informationCSc 372 Comparative Programming Languages. 4 : Haskell Basics
CSc 372 Comparative Programming Languages 4 : Haskell Basics Christian Collberg Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2011 Christian Collberg August 23, 2011
More informationRecursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions
Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions Greg Plaxton Theory in Programming Practice, Spring 2005 Department of Computer Science University of Texas at Austin
More informationCS 457/557: Functional Languages
CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University 1 Why Lists? Lists are a heavily used data structure in many functional programs Special syntax is
More informationHaskell Revision and Exercises
and Exercises April 10, 2017 Martin Perešíni Haskell Revision and Exercises April 10, 2017 1 / 53 Content Content Content Haskell Revision Types and Typeclasses Types of Functions List of types Syntax
More informationCPS 506 Comparative Programming Languages. Programming Language Paradigm
CPS 506 Comparative Programming Languages Functional Programming Language Paradigm Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming
More informationProgramming Paradigms
PP 2017/18 Unit 12 Functions and Data Types in Haskell 1/45 Programming Paradigms Unit 12 Functions and Data Types in Haskell J. Gamper Free University of Bozen-Bolzano Faculty of Computer Science IDSE
More informationAbout the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming
About the Tutorial Haskell is a widely used purely functional language. Functional programming is based on mathematical functions. Besides Haskell, some of the other popular languages that follow Functional
More informationCOSE212: Programming Languages. Lecture 3 Functional Programming in OCaml
COSE212: Programming Languages Lecture 3 Functional Programming in OCaml Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 3 September 18, 2017 1 / 44 Why learn ML? Learning ML is a good way of
More informationLecture 2: List algorithms using recursion and list comprehensions
Lecture 2: List algorithms using recursion and list comprehensions Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense September 12, 2017 Expressions, patterns
More informationTopic 6: Partial Application, Function Composition and Type Classes
Topic 6: Partial Application, Function Composition and Type Classes Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 11.11, 11.12 12.30, 12.31,
More informationTopic 6: Partial Application, Function Composition and Type Classes
Topic 6: Partial Application, Function Composition and Type Classes 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 11.11, 11.12 12.30, 12.31,
More informationLecture 4: Higher Order Functions
Lecture 4: Higher Order Functions Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense September 26, 2017 HIGHER ORDER FUNCTIONS The order of a function
More informationLecture 8: Summary of Haskell course + Type Level Programming
Lecture 8: Summary of Haskell course + Type Level Programming Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense October 31, 2017 Principles from Haskell
More informationOverview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping
Overview Declarative Languages D7012E Lecture 4: The Haskell type system Fredrik Bengtsson / Johan Nordlander Overloading & polymorphism Type classes instances of type classes derived type classes Type
More informationPROGRAMMING IN HASKELL. Chapter 2 - First Steps
PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 Glasgow Haskell Compiler GHC is the leading implementation of Haskell, and comprises a compiler and interpreter; The interactive nature of the interpreter
More informationSML A F unctional Functional Language Language Lecture 19
SML A Functional Language Lecture 19 Introduction to SML SML is a functional programming language and acronym for Standard d Meta Language. SML has basic data objects as expressions, functions and list
More informationHaskell Overview II (2A) Young Won Lim 8/23/16
(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
More informationGHCi: Getting started (1A) Young Won Lim 6/3/17
GHCi: Getting started (1A) Copyright (c) 2016-2017 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
More informationFunctional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph
Topics Functional Programming Christian Sternagel Harald Zankl Evgeny Zuenko Department of Computer Science University of Innsbruck WS 2017/2018 abstract data types, algebraic data types, binary search
More informationData types for mcrl2
Data types for mcrl2 Aad Mathijssen April 5, 2018 We provide a syntax for the standard data types of the mcrl2 language. This syntax is intended to be a practical mix between standard mathematical notation
More informationHaskell: yet another Functional Language. Distributed and Parallel Technology
Distributed and Parallel Technology Introducing Haskell Hans-Wolfgang Loidl http://www.macs.hw.ac.uk/~hwloidl School of Mathematical and Computer Sciences Heriot-Watt University, Edinburgh Haskell: yet
More information