Logic - CM0845 Introduction to Haskell

Similar documents
Logic - CM0845 Introduction to Prolog

Functional Programming and Haskell

Programming with Math and Logic

Lecture 4: Higher Order Functions

Exercise 1 (2+2+2 points)

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

CS 440: Programming Languages and Translators, Spring 2019 Mon

CSCE 314 Programming Languages

CS 320: Concepts of Programming Languages

Shell CSCE 314 TAMU. Higher Order Functions

Introduction to Functional Programming in Haskell 1 / 56

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

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

The Algebra of Programming in Haskell

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

CS 11 Haskell track: lecture 1

ML Built-in Functions

Imperative languages

Structural polymorphism in Generic Haskell

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

CS 457/557: Functional Languages

Haskell An Introduction

Functional Programming in C++

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

A general introduction to Functional Programming using Haskell

CSE 3302 Programming Languages Lecture 8: Functional Programming

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

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh

INTRODUCTION TO HASKELL

Lecture 10 September 11, 2017

Standard prelude. Appendix A. A.1 Classes

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

301AA - Advanced Programming

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

Type-indexed functions in Generic Haskell

Programming Language Concepts, CS2104 Lecture 7

Graphical Untyped Lambda Calculus Interactive Interpreter

Haskell Refresher Informatics 2D

Programming Languages

Programming Paradigms

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

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

Dependent Polymorphism. Makoto Hamana

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

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

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

Advanced features of Functional Programming (Haskell)

Exercise 1 ( = 22 points)

Lecture 5: Lazy Evaluation and Infinite Data Structures

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Exercises on ML. Programming Languages. Chanseok Oh

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

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10

Programming Languages 3. Definition and Proof by Induction

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

CSC324 Principles of Programming Languages

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

IA014: Advanced Functional Programming

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

Programming Language Concepts: Lecture 14

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Lecture 19: Functions, Types and Data Structures in Haskell

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh

Haskell: From Basic to Advanced. Part 3 A Deeper Look into Laziness

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

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

CSC324 Principles of Programming Languages

LECTURE 16. Functional Programming

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

Lecture 8: Summary of Haskell course + Type Level Programming

Parallel Haskell on MultiCores and Clusters

Introduction to Functional Programming using Haskell

Exercise 1 ( = 24 points)

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

CSE 130 [Winter 2014] Programming Languages

Functional Programming in Haskell for A level teachers

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

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

An introduction to functional programming. July 23, 2010

CSE 130 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

Recap: ML s Holy Trinity

CS 320: Concepts of Programming Languages

CSE399: Advanced Programming. Handout 2

CS 340 Spring 2019 Midterm Exam

Shell CSCE 314 TAMU. Functions continued

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

Programming Languages Fall 2013

Exercise 1 ( = 18 points)

Programming Languages

Continuation Passing Style. Continuation Passing Style

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Homework 3 COSE212, Fall 2018

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

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

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

Deriving Generic Functions by Example

Transcription:

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 2016-1 1 / 15

What is Haskell? Haskell is a purely functional programming language. That means that every function in Haskell is also a function in the mathematical sense. factorial 0 = 1 factorial n = n * factorial (n - 1) Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 2 / 15

Functions factorial 0 = 1 factorial n = n * factorial (n - 1) Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 3 / 15

Functions factorial 0 = 1 factorial n = n * factorial (n - 1) What is the type of this function? Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 3 / 15

Functions factorial 0 = 1 factorial n = n * factorial (n - 1) What is the type of this function? factorial :: Int -> Int factorial 0 = 1 factorial n = n * factorial (n - 1) Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 3 / 15

Functions factorial 0 = 1 factorial n = n * factorial (n - 1) What is the type of this function? factorial :: Int -> Int factorial 0 = 1 factorial n = n * factorial (n - 1) But 1 is an Integer, so... Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 3 / 15

Functions A solution for this bug: factorial :: Int -> Int factorial n n == 0 = 1 n > 0 = n * factorial (n - 1) otherwise = error "factorial: n < 0" Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 4 / 15

Functions A solution for this bug: factorial :: Int -> Int factorial n n == 0 = 1 n > 0 = n * factorial (n - 1) otherwise = error "factorial: n < 0" There are more than you believe! Google for The evolution of a Haskell programmer. Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 4 / 15

Lists Inductive definition Haskell has a built-in syntax for lists, where a list is either: the empty list, written [ ], or an element x and a list xs, written ( x : xs ). Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 5 / 15

Lists - Pattern matching on lists length :: [Int] -> Int length [] = 0 length (x : xs) = 1 + length xs Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 6 / 15

Lists - Pattern matching on lists length :: [Int] -> Int length [] = 0 length (x : xs) = 1 + length xs What if one wanted to get the length of a list of Booleans? Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 6 / 15

Lists - Pattern matching on lists length :: [Int] -> Int length [] = 0 length (x : xs) = 1 + length xs What if one wanted to get the length of a list of Booleans? length :: [Bool] -> Int length [] = 0 length (x : xs) = 1 + length xs Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 6 / 15

Lists - Pattern matching on lists length :: [Int] -> Int length [] = 0 length (x : xs) = 1 + length xs What if one wanted to get the length of a list of Booleans? length :: [Bool] -> Int length [] = 0 length (x : xs) = 1 + length xs Take it easy, there s another solution! Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 6 / 15

Parametric Polymorphism - Basic functions -- Returns the length of a finite list as an Int. length :: [a] -> Int -- Appends two lists. (++) :: [a] -> [a] -> [a] -- Extracts the first element of a list. head :: [a] -> a -- Extracts the last element of a list. last :: [a] -> a Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 7 / 15

- Basic functions -- Extracts the elements after the head of a list. tail :: [a] -> [a] -- Returns all the elements of a list except -- the last one. init :: [a] -> [a] -- Testes if a list is empty. null :: [a] -> Bool Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 8 / 15

Lazy Haskell wont t execute functions or calculate things until necessary. foo :: Int -> Bool -- Non-terminating function. foo n = foo (n + 1) bar :: Int -> Bool bar n = True foo n bar :: Int -> Bool bar n = foo n True Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 9 / 15

Lazy Haskell wont t execute functions or calculate things until necessary. foo :: Int -> Bool -- Non-terminating function. foo n = foo (n + 1) bar :: Int -> Bool bar n = True foo n bar :: Int -> Bool bar n = foo n True Try to calculate bar 3. Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 9 / 15

Lazy Haskell wont t execute functions or calculate things until necessary. foo :: Int -> Bool -- Non-terminating function. foo n = foo (n + 1) bar :: Int -> Bool bar n = True foo n bar :: Int -> Bool bar n = foo n True Try to calculate bar 3. Try to calculate bar 3. Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 9 / 15

High-Order Functions and Currying Every function in Haskell officially only takes one parameter. So how can we define a function that takes more than a parameter? -- Takes two things that can be ordered and returns the greater one. max :: (Ord a) => a -> a -> a max 2 3 iego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 10 / 15

High-Order Functions and Currying Every function in Haskell officially only takes one parameter. So how can we define a function that takes more than a parameter? -- Takes two things that can be ordered and returns the greater one. max :: (Ord a) => a -> a -> a max 2 3 (max 2) 3 iego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 10 / 15

High-Order Functions and Currying Every function in Haskell officially only takes one parameter. So how can we define a function that takes more than a parameter? -- Takes two things that can be ordered and returns the greater one. max :: (Ord a) => a -> a -> a max 2 3 (max 2) 3 Haskell functions can take functions as parameters and return functions as return values! iego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 10 / 15

High-Order Functions -- map f xs is the list obtained by applying f -- to each element of xs. map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x : xs) = f x : map f xs Which is the value of map (*2) [1, 2, 4]? Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 11 / 15

High-Order Functions -- map f xs is the list obtained by applying f -- to each element of xs. map :: (a -> b) -> [a] -> [b] map f [] = [] map f (x : xs) = f x : map f xs Which is the value of map (*2) [1, 2, 4]? GHCi> map (*2) [1, 2, 4] [2,4,8] Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 11 / 15

-- foldr, applied to a binary operator, a starting -- value and a list, reduces the list using th -- binary operator, from right to left (see also -- foldl): -- foldr f z [x1, x2,..., xn] == -- x1 f (x2 f... (xn f z)...) foldr :: (a -> b -> b) -> b -> [a] -> b foldr f z [] = z foldr f z (x : xs) = f x (foldr f z xs) Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 12 / 15

-- foldr, applied to a binary operator, a starting -- value and a list, reduces the list using th -- binary operator, from right to left (see also -- foldl): -- foldr f z [x1, x2,..., xn] == -- x1 f (x2 f... (xn f z)...) foldr :: (a -> b -> b) -> b -> [a] -> b foldr f z [] = z foldr f z (x : xs) = f x (foldr f z xs) GHCi> foldr (*) 1 [1..5] 120 Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 12 / 15

Creating Types - Algebraic Data Types data Bool = True False Functions by pattern-matching ( ) :: Bool -> Bool -> Bool True _ = True False x = x (&&) :: Bool -> Bool -> Bool False && _ = False True && x = x Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 13 / 15

Creating Types - Algebraic Data Types -- Recursive data type. data Nat = Zero Succ Nat Functions by pattern-matching (+) :: Nat -> Nat -> Nat Zero + n = n (Succ m) + n = Succ (m + n) -- Polymorphic data type. data List a = Nil Cons a (List a) Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 14 / 15

Some Links Real-World Applications See http://www.haskell.org/haskellwiki/haskell_ in_industry. Nice Tutorial See http://learnyouahaskell.com. Downloading See https://www.haskell.org/downloads. Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester 2016-1 15 / 15