Introduction to Haskell
|
|
- Ira Miles
- 5 years ago
- Views:
Transcription
1 Introduction to Haskell Matt Mullins Texas A&M Computing Society October 6, 2009 Matt Mullins (TACS) Introduction to Haskell October 6, / 39
2 Outline Introduction to Haskell Functional Programming Haskell Specifics Beginning Haskell Using Haskell First Bits of Syntax Slightly More Advanced Types A Real Haskell Program Core Logic Monads The Parser Bringing it all together Matt Mullins (TACS) Introduction to Haskell October 6, / 39
3 Outline Introduction to Haskell Introduction to Haskell Functional Programming Haskell Specifics Beginning Haskell Using Haskell First Bits of Syntax Slightly More Advanced Types A Real Haskell Program Core Logic Monads The Parser Bringing it all together Matt Mullins (TACS) Introduction to Haskell October 6, / 39
4 Introduction to Haskell What is functional programming? Functional Programming Instead of a series of modifications, functional programming uses immutable data. Variables contents never change Matt Mullins (TACS) Introduction to Haskell October 6, / 39
5 Introduction to Haskell What is functional programming? Functional Programming Instead of a series of modifications, functional programming uses immutable data. Variables contents never change Functions (i.e. pure functions) always return the same result when given the same input. There is no state when using pure functions the only state information is the function arguments. This is a more mathematical use of the word function, but it is still extremely usable for computer programs. Matt Mullins (TACS) Introduction to Haskell October 6, / 39
6 Introduction to Haskell Functional Programming Why should I use it? Easier to test, prevent bugs, etc Easier to prove correctness Haskell uses lazy evaluation: expressions are evaluated only when necessary, allowing data structures like the infinite list. Estimating theoretical performance can be easy, because you can use the mathematical definitions Can still use non-functional programming constructions more on that later Matt Mullins (TACS) Introduction to Haskell October 6, / 39
7 Why should I use it? Introduction to Haskell Functional Programming Easier to test, prevent bugs, etc Easier to prove correctness Haskell uses lazy evaluation: expressions are evaluated only when necessary, allowing data structures like the infinite list. Estimating theoretical performance can be easy, because you can use the mathematical definitions Can still use non-functional programming constructions more on that later Can theoretically be automatically made concurrent by the compiler Useful now that dual- and quad-core computers are common Still a subject of research: the functionality is nowhere near complete. It still probably won t help you. Matt Mullins (TACS) Introduction to Haskell October 6, / 39
8 Why shouldn t I use it? Introduction to Haskell Functional Programming Somewhat difficult to debug, since you can t step through code Lazy evaluation means your code isn t always evaluated in the same order. Can be hard to read someone else s code Estimating real performance can be difficult Some APIs don t have bindings for Haskell yet Matt Mullins (TACS) Introduction to Haskell October 6, / 39
9 Introduction to Haskell Haskell Specifics What sets Haskell apart (from other functional languages)? Main difference: everything is purely functional. Other functional languages like Scheme and OCaml allow you to modify variables after all. Lazy evaluation is impossible. Don t be scared. You can still do things that seem non-functional. Monads make this possible. Haskell is strongly-typed. Haskell has a well-designed module system, much like many newer languages such as Python and C#. Matt Mullins (TACS) Introduction to Haskell October 6, / 39
10 Outline Beginning Haskell Introduction to Haskell Functional Programming Haskell Specifics Beginning Haskell Using Haskell First Bits of Syntax Slightly More Advanced Types A Real Haskell Program Core Logic Monads The Parser Bringing it all together Matt Mullins (TACS) Introduction to Haskell October 6, / 39
11 Running the code Beginning Haskell Using Haskell Haskell code can be compiled or interpreted. Compiler: ghc Glasgow Haskell Compiler Interpreter: ghci or hugs Matt Mullins (TACS) Introduction to Haskell October 6, / 39
12 Running the code Beginning Haskell Using Haskell Haskell code can be compiled or interpreted. Compiler: ghc Glasgow Haskell Compiler Interpreter: ghci or hugs In this presentation: Lines that begin with Prelude> are the ghci interpreter Everything else would be put into a source code file. Matt Mullins (TACS) Introduction to Haskell October 6, / 39
13 Beginning Haskell First Bits of Syntax Variables and functions Variable definition uses the = operator. Prelude> let a = 3 Prelude> a 3 The let keyword is used only with the interpreter. Matt Mullins (TACS) Introduction to Haskell October 6, / 39
14 Beginning Haskell First Bits of Syntax Variables and functions Variable definition uses the = operator. Prelude> let a = 3 Prelude> a 3 The let keyword is used only with the interpreter. Function application is a space, instead of parentheses. Prelude> let f x = x+5 Prelude> f Matt Mullins (TACS) Introduction to Haskell October 6, / 39
15 Beginning Haskell First Bits of Syntax Functions with multiple parameters Very similar construction: Prelude> let g x y z = x+y-z Prelude> g How on earth can this work? Matt Mullins (TACS) Introduction to Haskell October 6, / 39
16 Beginning Haskell First Bits of Syntax Functions with multiple parameters Very similar construction: Prelude> let g x y z = x+y-z Prelude> g How on earth can this work? Functions are just as much values as anything else (like a number, for instance). Function application is an operator that binds more tightly than any other. It always takes exactly one parameter, and returns another function with one fewer parameter. It is left-associative. That is: f a b c ((f a) b) c Matt Mullins (TACS) Introduction to Haskell October 6, / 39
17 Beginning Haskell Slightly More Advanced Scope Very important, similar to structured languages like C++. What happens if: Prelude> let a = 1; b = 2 Prelude> let h a b = a+b Prelude> h 5 6 Matt Mullins (TACS) Introduction to Haskell October 6, / 39
18 Beginning Haskell Slightly More Advanced Scope Very important, similar to structured languages like C++. What happens if: Prelude> let a = 1; b = 2 Prelude> let h a b = a+b Prelude> h 5 6 The answer is 11, not 3. The a and b in the function refer to its arguments, not the ones already defined. Matt Mullins (TACS) Introduction to Haskell October 6, / 39
19 Beginning Haskell Slightly More Advanced Operators Infix operators are treated simply as functions. For example, (+) is simply a function that takes two numbers and returns the sum. Parentheses are important: they make an infix operator behave like a function name. You can make up your own operators, as long as they use only the following:!#$%&*+./<=>?@\^ -~ Matt Mullins (TACS) Introduction to Haskell October 6, / 39
20 Beginning Haskell Slightly More Advanced Pattern matching When you define a function, the left side is really a pattern that the compiler matches against. Variable names match anything, and their contents are available on the right side. Constructors and literals must match exactly. Patterns are matched top-to-bottom, until a match succeeds. Example: matching a list: count [] = 0 count (x:xs) = 1 + count xs Matt Mullins (TACS) Introduction to Haskell October 6, / 39
21 Various operators Beginning Haskell Slightly More Advanced Function composition: (.) :: (a->b) -> (c->a) -> c -> b (f. g) x f (g x) Function application: ($) :: (a->b) -> a -> b Why are these used? Matt Mullins (TACS) Introduction to Haskell October 6, / 39
22 Beginning Haskell Slightly More Advanced Unnamed functions Two main ways to do it: Lambda expressions: (\x -> x+3) Half-evaluated function (or operators): g 1 2 (3+) Matt Mullins (TACS) Introduction to Haskell October 6, / 39
23 Beginning Haskell Types Haskell type system Every expression in Haskell has an unambiguous type. You can explore the type of an expression using the :t interpreter command: Prelude> :t a a :: Integer Prelude> :t g g :: (Num a) => a -> a -> a -> a Most types are inferred from context (or syntax) A type doesn t have to be unique. :: is used to express the type of a particular expression. Matt Mullins (TACS) Introduction to Haskell October 6, / 39
24 Beginning Haskell Types Lists An element cons-ed with the rest of the list The cons operator is :, and the empty list is [], so 1:(2:(3:[])) is a list 1,2,3. : is right-associative, so the above is equivalent to 1:2:3:[]. There is syntactic sugar that does this for you; it is also equivalent to [1,2,3] A list can be as long as you want, but all the elements have to be of the same type. A type specification would look like [1,2,3] :: [Integer] Matt Mullins (TACS) Introduction to Haskell October 6, / 39
25 Beginning Haskell Types Tuples A tuple is a fixed-length collection of elements that can be treated as a single object. A tuple uses the syntax (1,2), which can be used in more complicated ways, like (1,2,(3, a )) A tuple can have elements of different types, unlike a list. The type specification looks like (1,2,(3, a )) :: (Integer, Integer, (Integer, Char)) Matt Mullins (TACS) Introduction to Haskell October 6, / 39
26 Defining your own types Beginning Haskell Types data IntList = IntItem Integer IntEnd Defines two constructors : IntItem :: Integer -> IntList and IntEnd :: IntList Types can have parameters like functions: data List a = Item a End This defines two constructors: Item :: a -> List a and End :: List a Note that End is simultaneously of an infinite number of types. Matt Mullins (TACS) Introduction to Haskell October 6, / 39
27 Defining your own types Beginning Haskell Types data IntList = IntItem Integer IntEnd Defines two constructors : IntItem :: Integer -> IntList and IntEnd :: IntList Types can have parameters like functions: data List a = Item a End This defines two constructors: Item :: a -> List a and End :: List a Note that End is simultaneously of an infinite number of types. You can also define type synonyms: type OtherList = List Char Matt Mullins (TACS) Introduction to Haskell October 6, / 39
28 Type classes Beginning Haskell Types A type class is a set of types that support the same operations. We ve already seen one type class already: Num. Other important classes are: Show: function show :: (Show a) => a -> String Eq: operator (==) :: (Eq a) => a -> a -> Bool Ord: supports operations to determine ordering (i.e. <, >, etc.) Allows for polymorphism. Matt Mullins (TACS) Introduction to Haskell October 6, / 39
29 Outline A Real Haskell Program Introduction to Haskell Functional Programming Haskell Specifics Beginning Haskell Using Haskell First Bits of Syntax Slightly More Advanced Types A Real Haskell Program Core Logic Monads The Parser Bringing it all together Matt Mullins (TACS) Introduction to Haskell October 6, / 39
30 A Real Haskell Program A course scheduler As course registration time nears, I want a program which can determine the schedule that is most ideal for me. My requirements: Large contiguous blocks of time outside of class Accepts input in a format that is easy to type and read Why I chose Haskell: The ability to derive new data types with minimal code Extremely easy to define a parser for a domain-specific language I tried it in C++ first, and the only algorithm I could come up with was recursive much easier to express in Haskell. Matt Mullins (TACS) Introduction to Haskell October 6, / 39
31 The data file format A Real Haskell Program Something easy to type into a text file; example: CPSC 221{ 501{ MWF 11:30 12:20 HRBB 124; TR 15:55 16:45 HRBB 232; } 502{ MWF 11:30 12:20 HRBB 124; MW 12:40 13:30 HRBB 232; } } ENGL 301{ 570{MWF 13:50 14:40 BLOC 114; } } Matt Mullins (TACS) Introduction to Haskell October 6, / 39
32 A Real Haskell Program The core data structures Core Logic data Weekday = M T W R F deriving (Eq, Ord, Show) data Period = Period { day :: Weekday, start, end :: Time } deriving (Eq, Ord) data Section = Section { course :: Course, number :: Int, periods :: [Period] } deriving Eq data Course = Course { desig :: String, sections :: [Section] } deriving (Eq, Show) Matt Mullins (TACS) Introduction to Haskell October 6, / 39
33 A Real Haskell Program Determining overlapping periods Core Logic overlap1 :: Period -> Period -> Bool overlap1 p q = (start p >= start q && start p <= end q) (end p >= start q && end p <= end q) overlap :: Period -> Period -> Bool overlap p q = (day p == day q) && (overlap1 p q overlap1 q p) Matt Mullins (TACS) Introduction to Haskell October 6, / 39
34 A Real Haskell Program Determining conflicting sections Core Logic conflp :: [Period] -> [Period] -> Bool conflp (x:xs) ys = any (overlap x) ys conflp xs ys conflp [] _ = False conflicts :: Section -> Section -> Bool conflicts a b = conflp (periods a) (periods b) conflict :: [Section] -> Bool conflict (s:ss) = any (conflicts s) ss conflict ss conflict [] = False Matt Mullins (TACS) Introduction to Haskell October 6, / 39
35 A Real Haskell Program Core Logic Choosing one section from each class choosells :: [[Section]] -> [[Section]] choosells (c:cs) = concat [map (x:) $ choosells cs x <- c] choosells [] = [[]] choosells takes its input as one list element per course, and its output is one list element per possible schedule. Matt Mullins (TACS) Introduction to Haskell October 6, / 39
36 A Real Haskell Program Core Logic Choosing one section from each class choosells :: [[Section]] -> [[Section]] choosells (c:cs) = concat [map (x:) $ choosells cs x <- c] choosells [] = [[]] choosells takes its input as one list element per course, and its output is one list element per possible schedule. choose :: [Course] -> [[Section]] choose cs = choosells (map sections cs) Matt Mullins (TACS) Introduction to Haskell October 6, / 39
37 A Real Haskell Program Which schedule is the best? Core Logic score ps = let byday = [filter ((d==). day) ps d <- [M,T,W,R,F]] time [] = 0 time l = fromintegral $ (end $ last l) - (start $ head l) times = map time byday :: [Float] meantime = (sum times) / 5.0 in sum $ map (\x -> (x-meantime)^2) times This is the variance of the length of each day. Matt Mullins (TACS) Introduction to Haskell October 6, / 39
38 A Real Haskell Program Monads What is a Monad? A monad is some type M that supports the two operations: Promoting an expression: return :: a -> M a Combining monads: (>>=) :: M a -> (a->m b) -> M b. Note that a monadic type must have a single type argument. There is no function (in the general case) to unbox a monad. The way in which the (>>=) operator combines monads is defined by the individual monad. Matt Mullins (TACS) Introduction to Haskell October 6, / 39
39 A Real Haskell Program Monads What is a Monad? A monad is some type M that supports the two operations: Promoting an expression: return :: a -> M a Combining monads: (>>=) :: M a -> (a->m b) -> M b. Note that a monadic type must have a single type argument. There is no function (in the general case) to unbox a monad. The way in which the (>>=) operator combines monads is defined by the individual monad. Allows for stateful computation in a purely functional world. Began life as an abuse of the Haskell type system, and quickly gained status as one of its most distinctive features. Matt Mullins (TACS) Introduction to Haskell October 6, / 39
40 The IO Monad A Real Haskell Program Monads The type IO a represents an action whose value is of type a. The combination operator performs the actions in sequence. Useful functions: putstr :: String -> IO () putstrln :: String -> IO () getchar :: IO Char getline :: IO String getcontents :: IO String Matt Mullins (TACS) Introduction to Haskell October 6, / 39
41 A Real Haskell Program Monads The Parser Monad From the parsec library The type GenParser tok st val represents a parser which turns [tok] into something of type val Usually use a Parser GenParser Char () Combination operator creates a parser that matches in sequence You can break out of a Parser, but only in a purely functional way: parse :: GenParser tok () a -> String -> [tok] -> a Matt Mullins (TACS) Introduction to Haskell October 6, / 39
42 do syntax A Real Haskell Program Monads do s <- getline putstr "Hello, " putstrln s is syntactic sugar for getline >>= (\s -> putstr "Hello, " >> putstrln s) Matt Mullins (TACS) Introduction to Haskell October 6, / 39
43 do syntax A Real Haskell Program Monads do s <- getline putstr "Hello, " putstrln s is syntactic sugar for getline >>= (\s -> putstr "Hello, " >> putstrln s) With this syntax, you can almost forget it s purely functional Matt Mullins (TACS) Introduction to Haskell October 6, / 39
44 The small bits A Real Haskell Program The Parser time = do hour <- many1 digit char : minute <- many1 digit return (t (strtoint hour) (strtoint minute)) period = do date <- many1 $ oneof [ M, T, W, R, F ] spaces start <- time spaces end <- time spaces char ; spaces return [Period (chartoweekday d) start end d <- date] Matt Mullins (TACS) Introduction to Haskell October 6, / 39
45 Match a section A Real Haskell Program The Parser sectionnumber = many1 (digit) >>= return. stringtoint section = do num <- sectionnumber spaces char { spaces periods <- many1 period spaces char } spaces return (num, concat periods) Matt Mullins (TACS) Introduction to Haskell October 6, / 39
46 And now a course A Real Haskell Program The Parser coursep = do name <- coursename char { spaces sections <- many1 section spaces char } spaces (let c = Course name s s = [Section c n p (n,p) <- sections] in return c) courses = many1 coursep Matt Mullins (TACS) Introduction to Haskell October 6, / 39
47 Sorry A Real Haskell Program The Parser Sorry, guys, that huge block of code was boring. My apologies. It was easy to write, though, which is why I used Haskell Matt Mullins (TACS) Introduction to Haskell October 6, / 39
48 Output function A Real Haskell Program Bringing it all together putsingle :: [Section] -> IO () putsingle [] = putstrln "" putsingle (s:ss) = do putstr (show s) putsingle ss putsched :: [(Score, [Section])] -> IO () putsched [] = putstrln "" putsched ((sc,s):ss) = do putstr (show sc) putstr "\t" putsingle s putsched ss Matt Mullins (TACS) Introduction to Haskell October 6, / 39
49 The main expression A Real Haskell Program Bringing it all together scoresched :: [Section] -> Int scoresched x = score. sort. concat. map periods $ x dowork :: [Course] -> IO () dowork cs = let sch = schedule cs ss = map (\s -> (scoresched s, s)) sch sed = sortby (comparing fst) ss in putsched sed main = do s <- getcontents (case parse courses "stdin" s of (Right cs) -> dowork cs (Left cs) -> print cs) Matt Mullins (TACS) Introduction to Haskell October 6, / 39
CS 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 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 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 informationMonad Background (3A) Young Won Lim 11/18/17
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 or any later version published
More informationMonad Background (3A) Young Won Lim 11/8/17
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 or any later version published
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 informationProgramming in Haskell Aug Nov 2015
Programming in Haskell Aug Nov 2015 LECTURE 23 NOVEMBER 12, 2015 S P SURESH CHENNAI MATHEMATICAL INSTITUTE Summary of IO Actions of type IO t1, t1 -> IO t2, t1 -> t2 -> IO t3 etc. As opposed to pure functions
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 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 informationSide Effects (3B) Young Won Lim 11/23/17
Side Effects (3B) 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 or any later
More informationSide Effects (3B) Young Won Lim 11/20/17
Side Effects (3B) 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 or any later
More informationSide Effects (3B) Young Won Lim 11/27/17
Side Effects (3B) 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 or any later
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 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 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 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 informationMonad Background (3A) Young Won Lim 10/5/17
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 or any later version published
More informationPractical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.
Practical Practical An introduction to functional programming July 21, 2011 Contents Practical Practical is fun, and that s what it s all about! Even if seems strange to you at first, don t give up. Learning
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 informationInformatics 1 Functional Programming Lectures 15 and 16. IO and Monads. Don Sannella University of Edinburgh
Informatics 1 Functional Programming Lectures 15 and 16 IO and Monads Don Sannella University of Edinburgh Part I The Mind-Body Problem The Mind-Body Problem Part II Commands Print a character putchar
More informationThe List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona
The List Datatype CSc 372 Comparative Programming Languages 6 : Haskell Lists Department of Computer Science University of Arizona collberg@gmail.com All functional programming languages have the ConsList
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 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 informationCMSC 330: Organization of Programming Languages. Functional Programming with Lists
CMSC 330: Organization of Programming Languages Functional Programming with Lists CMSC330 Spring 2018 1 Lists in OCaml The basic data structure in OCaml Lists can be of arbitrary length Implemented as
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 informationI/O in Purely Functional Languages. Stream-Based I/O. Continuation-Based I/O. Monads
I/O in Purely Functional Languages Stream-Based I/O Four centuries ago, Descartes pondered the mind-body problem: how can incorporeal minds interact with physical bodies? Designers of purely declarative
More informationCS 440: Programming Languages and Translators, Spring 2019 Mon 2/4
Haskell, Part 5 CS 440: Programming Languages and Translators, Spring 2019 Mon 2/4 More Haskell Miscellaneous topics Simple I/O, do blocks, actions Modules data vs type declaration Instances of classtypes
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 informationCSCI-GA Scripting Languages
CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation
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 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 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 informationHaskell 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 informationCSCE 314 Programming Languages. Interactive Programming: I/O
CSCE 314 Programming Languages Interactive Programming: I/O Dr. Hyunyoung Lee 1 Introduction To date, we have seen how Haskell can be used to write batch programs that take all their inputs at the start
More informationInformatics 1 Functional Programming 19 Tuesday 23 November IO and Monads. Philip Wadler University of Edinburgh
Informatics 1 Functional Programming 19 Tuesday 23 November 2010 IO and Monads Philip Wadler University of Edinburgh The 2010 Informatics 1 Competition Sponsored by Galois (galois.com) List everyone who
More informationCMSC 330: Organization of Programming Languages. Functional Programming with Lists
CMSC 330: Organization of Programming Languages Functional Programming with Lists 1 Lists in OCaml The basic data structure in OCaml Lists can be of arbitrary length Implemented as a linked data structure
More informationSide Effects (3A) Young Won Lim 1/13/18
Side Effects (3A) 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 or any later
More informationIO Monad (3C) Young Won Lim 1/6/18
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 or any later version published
More informationHaskell Syntax in Functions
Haskell Syntax in Functions http://igm.univ-mlv.fr/~vialette/?section=teaching Stéphane Vialette LIGM, Université Paris-Est Marne-la-Vallée December 14, 2015 Syntax in Functions Pattern Matching Pattern
More informationPROGRAMMING IN HASKELL. Chapter 10 - Interactive Programming
PROGRAMMING IN HASKELL Chapter 10 - Interactive Programming 0 Introduction To date, we have seen how Haskell can be used to write batch programs that take all their inputs at the start and give all their
More informationProgramming with Math and Logic
.. Programming with Math and Logic an invitation to functional programming Ed Morehouse Wesleyan University The Plan why fp? terms types interfaces The What and Why of Functional Programming Computing
More informationOverloading, Type Classes, and Algebraic Datatypes
Overloading, Type Classes, and Algebraic Datatypes Delivered by Michael Pellauer Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 28, 2006 September 28, 2006 http://www.csg.csail.mit.edu/6.827
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 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 Monads CSC 131. Kim Bruce
Haskell Monads CSC 131 Kim Bruce Monads The ontological essence of a monad is its irreducible simplicity. Unlike atoms, monads possess no material or spatial character. They also differ from atoms by their
More informationIO Monad (3D) Young Won Lim 1/18/18
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 or any later version published
More informationHaskell Making Our Own Types and Typeclasses
Haskell Making Our Own Types and Typeclasses http://igm.univ-mlv.fr/~vialette/?section=teaching Stéphane Vialette LIGM, Université Paris-Est Marne-la-Vallée January 13, 2015 Making Our Own Types and Typeclasses
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 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 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 informationEECS 700 Functional Programming
EECS 700 Functional Programming Dr. Andy Gill University of Kansas February 16, 2010 1 / 41 Parsing A parser is a program that analyses a piece of text to determine its syntactic structure. The expression
More informationIO Monad (3C) Young Won Lim 8/23/17
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 or any later version published
More informationCS457/557 Functional Languages
CS457/557 Functional Languages Spring 2018 Lecture 1: Course Introduction Andrew Tolmach Portland State University (with thanks to Mark P. Jones) 1 Goals of this course Introduce the beautiful ideas of
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 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 informationHaskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.
Haskell: Lists CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks
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 informationParsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics
National Institute of Informatics May 31, June 7, June 14, 2010 All Right Reserved. Outline I 1 Parser Type 2 Monad Parser Monad 3 Derived Primitives 4 5 6 Outline Parser Type 1 Parser Type 2 3 4 5 6 What
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 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 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 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 information1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.
1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 2. true / false ML can be compiled. 3. true / false FORTRAN can reasonably be considered
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 informationCS 209 Functional Programming
CS 209 Functional Programming Lecture 03 - Intro to Monads Dr. Greg Lavender Department of Computer Science Stanford University "The most important thing in a programming language is the name. A language
More informationLazy Functional Programming in Haskell
Lazy Functional Programming in Haskell David Raymond Christiansen 25 November, 2013 What is Haskell? 2 What is Haskell? Pure functional language: no side effects 2 What is Haskell? Pure functional language:
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 informationThis example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator
1 2 This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator (total) and a counter (i); it works by assigning
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 informationA Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1
A Second Look At ML Chapter Seven Modern Programming Languages, 2nd ed. 1 Outline Patterns Local variable definitions A sorting example Chapter Seven Modern Programming Languages, 2nd ed. 2 Two Patterns
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 informationProgramming in Haskell Aug-Nov 2015
Programming in Haskell Aug-Nov 2015 LECTURE 14 OCTOBER 1, 2015 S P SURESH CHENNAI MATHEMATICAL INSTITUTE Enumerated data types The data keyword is used to define new types data Bool = False True data Day
More informationFall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives
CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write
More informationHaskell & functional programming, some slightly more advanced stuff. Matteo Pradella
Haskell & functional programming, some slightly more advanced stuff Matteo Pradella pradella@elet.polimi.it IEIIT, Consiglio Nazionale delle Ricerche & DEI, Politecnico di Milano PhD course @ UniMi - Feb
More informationSummer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define
CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More informationCS 457/557: Functional Languages
CS 457/557: Functional Languages I/O Actions in Haskell Mark P Jones Portland State University 1 Question: If functional programs don t have any side-effects, then how can we ever do anything useful? 2
More informationFunctional Logic Programming Language Curry
Functional Logic Programming Language Curry Xiang Yin Department of Computer Science McMaster University November 9, 2010 Outline Functional Logic Programming Language 1 Functional Logic Programming Language
More informationIntroduction to OCaml
Fall 2018 Introduction to OCaml Yu Zhang Course web site: http://staff.ustc.edu.cn/~yuzhang/tpl References Learn X in Y Minutes Ocaml Real World OCaml Cornell CS 3110 Spring 2018 Data Structures and Functional
More informationSCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015
SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
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 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 informationCS 320: Concepts of Programming Languages
CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 04: Basic Haskell Continued o Polymorphic Types o Type Inference with Polymorphism o Standard
More informationHarvard School of Engineering and Applied Sciences CS 152: Programming Languages
Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 18 Thursday, March 29, 2018 In abstract algebra, algebraic structures are defined by a set of elements and operations
More informationCIS552: Advanced Programming
CIS552: Advanced Programming Handout 8 What is a Parser? A parser is a program that analyzes a piece of text to deine its structure (and, typically, returns a tree representing this structure). The World
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 informationCSc 372. Comparative Programming Languages. 3 : Haskell Introduction. Department of Computer Science University of Arizona
1/17 CSc 372 Comparative Programming Languages 3 : Haskell Introduction Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/17 What is Haskell?
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 informationCIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL
CIS 194: Homework 3 Due Wednesday, February 11, 2015 Interpreters An interpreter is a program that takes another program as an input and evaluates it. Many modern languages such as Java 1, Javascript,
More informationSpring 2018 Discussion 7: March 21, Introduction. 2 Primitives
CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme
More informationBox-and-arrow Diagrams
Box-and-arrow Diagrams 1. Draw box-and-arrow diagrams for each of the following statements. What needs to be copied, and what can be referenced with a pointer? (define a ((squid octopus) jelly sandwich))
More informationCPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia
CPL 2016, week 10 Clojure functional core Oleg Batrashev Institute of Computer Science, Tartu, Estonia April 11, 2016 Overview Today Clojure language core Next weeks Immutable data structures Clojure simple
More information7. Introduction to Denotational Semantics. Oscar Nierstrasz
7. Introduction to Denotational Semantics Oscar Nierstrasz Roadmap > Syntax and Semantics > Semantics of Expressions > Semantics of Assignment > Other Issues References > D. A. Schmidt, Denotational Semantics,
More informationCS 457/557: Functional Languages
CS 457/557: Functional Languages From Trees to Type Classes Mark P Jones Portland State University 1 Trees:! " There are many kinds of tree data structure.! " For example: data BinTree a = Leaf a BinTree
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 98 in short! CPSC 449 Principles of Programming Languages
Haskell 98 in short! n Syntax and type inferencing similar to ML! n Strongly typed! n Allows for pattern matching in definitions! n Uses lazy evaluation" F definition of infinite lists possible! n Has
More informationI/O in Haskell. To output a character: putchar :: Char -> IO () e.g., putchar c. To output a string: putstr :: String -> IO () e.g.
I/O in Haskell Generally, I/O functions in Haskell have type IO a, where a could be any type. The purpose and use of a will be explained later. We call these commands or actions, for we think of them as
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 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 informationMonad Background (3A) Young Won Lim 11/20/17
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 or any later version published
More informationHaskell A Wild Ride. Abstract. 1 Safety Precautions. Sven Moritz Hallberg
Haskell A Wild Ride Sven Moritz Hallberg 21st. Chaos Communication Congress Berlin, December 27th 29th 2004 Abstract The Haskell programming language is a comparatively young purely functional
More information