Haskell Types COMP360

Similar documents
Haskell Types COMP360

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

Haskell An Introduction

Programming Languages Fall 2013

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

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

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

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

Standard prelude. Appendix A. A.1 Classes

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

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

Haskell Review COMP360

FUNCTIONAL PROGRAMMING 1 HASKELL BASICS

Functional Programming and Haskell

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Introduction to Programming, Aug-Dec 2006

FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS. Tatsuya Hagino

Haskell through HUGS THE BASICS

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Functional Programming in Haskell Part I : Basics

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

Programming in Haskell Aug-Nov 2015

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

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

CSC324 Principles of Programming Languages

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

A Brief Introduction to Standard ML

Introduction to Programming: Lecture 3

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

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

CS 11 Haskell track: lecture 1

CMSC 330, Fall 2013, Practice Problems 3

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

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

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

CS 320: Concepts of Programming Languages

Introduction to Objective Caml

Course year Typeclasses and their instances

Why OCaml? Introduction to Objective Caml. Features of OCaml. Applications written in OCaml. Stefan Wehr

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

Topic 5: Higher Order Functions

Topic 5: Higher Order Functions

CSc 520. Principles of Programming Languages 11: Haskell Basics

Functional Programming for Logicians - Lecture 1

Custom Types. Outline. COMP105 Lecture 19. Today Creating our own types The type keyword The data keyword Records

A general introduction to Functional Programming using Haskell

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

Topic 7: Algebraic Data Types

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Programming Paradigms

INTRODUCTION TO HASKELL

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

CS 440: Programming Languages and Translators, Spring 2019 Mon 2/4

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

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

Software System Design and Implementation

Data Types The ML Type System

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

Learn You a Haskell for Great Good!

PAL Sessions: COMP1100 focus: 3pm daily, GEOG 202 COMP1130 focus: 1pm Thursday, GEOG 202

Introduction to Haskell

Lecture 2: List algorithms using recursion and list comprehensions

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

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Exercises on ML. Programming Languages. Chanseok Oh

CS 440: Programming Languages and Translators, Spring 2019 Mon

Mini-ML. CS 502 Lecture 2 8/28/08

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

CS 320: Concepts of Programming Languages

Exercise 1 ( = 24 points)

Overloading, Type Classes, and Algebraic Datatypes

CS558 Programming Languages

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

Programming Language Concepts: Lecture 14

Lazy Functional Programming in Haskell

Logical Methods in... using Haskell Getting Started

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

Lecture: Functional Programming

Advanced features of Functional Programming (Haskell)

Topic 9: Type Checking

Topic 9: Type Checking

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

Programming Paradigms

Imperative languages

CMPT 125: Lecture 3 Data and Expressions

CSC324 Principles of Programming Languages

Haskell Making Our Own Types and Typeclasses

Quiz 1, January 15, minutes; ½ pt/answer; 2½ pts total. 1. In what decade was the oldest language still in use created?

CSE 3302 Programming Languages Lecture 8: Functional Programming

Haskell Syntax in Functions

Functional Programming

Exercise 1 ( = 18 points)

CS 360: Programming Languages Lecture 10: Introduction to Haskell

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

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Programming in Haskell Aug-Nov 2015

Haskell 98 in short! CPSC 449 Principles of Programming Languages

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

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Transcription:

Haskell Types COMP360

No computer has ever been designed that is ever aware of what it's doing; but most of the time, we aren't either. Marvin Minsky

Haskell Programming Assignment A Haskell programming assignment has been posted on Blackboard You have to write 8 simple Haskell functions Upload one.hs file to Blackboard by noon on Tuesday, April 4, 2017

Comments In Haskell comments start with two dashes and run until the end of the line -- This is a comment Please put comments in all Haskell files you submit giving your name and a brief explanation of the method

Tuples Tuples are like list but they can have different data types Tuples are written with parenthesis (5,"cow") (1,"aardvard") When using multiple tuples, they must have elements of the same type

Tuple Functions fst returns the first element of a tuple snd returns the second element of a tuple zip given two lists, returns a list of tuples with elements from the first list matched with the second list zip [1,2,3] [ 'X', 'Y', 'Z'] returns [(1,'X'),(2,'Y'),(3,'Z')]

Data Types Haskell is strongly typed. Each variable and function has a specific data type You do not have to declare data types. Haskell infers the data type by how it is used You can specify the types in Haskell and often it makes your intentions clearer to the compiler

Usual Simple Data Types Int whole numbers Integer whole numbers that can be very large Double floating point numbers Bool true or false Char character Datatype names all start with a CAPITAL letter

List and Tuples Both lists and tuples define types [Char] is a list of Char and can be called String A tuple specifies the type of all elements (Int, Char) is a tuple that has an int as the first value and a single character as the second, i.e. (5, Z )

Write a Haskell Function Write a simple Haskell function to reverse a list without using the built-int reverse function myrev "Williams" will return "smailliw"

Defining Function Types You can define the type of a function and its parameters myreverse :: [a] -> [a] myreverse lst = if length lst == 1 then etc. This specifies that myreverse takes a list of objects and returns a list of the same type of objects

Type Inference The Haskell system is very good at determining the data type you intended for your program If your program does arithmetic with a parameter, Haskell recognizes that the parameters must be numbers

Generic Type Definitions You can specify the type of a function without specifying the type of the parameters The type specification of the built-in tail function is tail :: [a] -> [a] The letter "a" is a type variable Type variables are usually one letter For tail, "a" represents an undefined type. It will work for any type tail is polymorphic

Haskell Practice Write a Haskell function that takes two arrays of numbers and returns the sum of the each pair of elements multiplied together a1 * b1 + a2 * b2 + a3 * b3 + etc. dot :: [a] -> [a] -> a

Typeclasses A typeclass groups the different types based on functionality All types in a typeclass can perform a specific action

Some Haskell Typeclasses Eq is used for types that support equality testing Ord is for types that have an ordering. Show can be presented as strings Read takes a value out of a string Num are numbers Integral are whole numbers

Defining Function Types with Typeclasses You can specifically define the classtype of a parameter sumsqr :: Num a => [a] -> a sumsqr lst = if length lst == 0 then etc. The typeclass specified to the left of the => is called a class constraint The type definition states that the function takes a list of numerical values and returns a number

Write Haskel Type Specification Write Haskel type specification for these functions trim Remove leading spaces from a string count - Count the occurrence of a character is a string

Possible Solutions Write Haskel type specification for these functions trim Remove leading spaces from a string trim :: [Char] -> [Char] count - Count the occurrence of a character is a string count :: (Eq a1, Num a) => [a1] -> a1 -> a

Checking Types The :t command in GHCi will give you the type of data or a function :t "Sample stuff" "Sample stuff" :: [Char] :t trim trim :: [Char] -> [Char]

Practice Create a Haskell function to sum a list of list of numbers sumlist [[4,5,6], [1,2]] returns 18 sumlist :: Num a => [[a]] -> a

Functions as Parameters A function can be passed to another function as a parameter Consider this function which does anything to a list then univ lst func = func lst univ [2,1,7] length returns 3 univ [2,1,7] sum returns 10 univ [2,1,7] sumsqr returns 54

Haskell Patterns You can write multiple definitions of a function Haskell will consider them in order and execute the first on that fits You can put constants as the parameters in the definition. If you call the function with that value, it will execute that function definition

Fibonacci Pattern Example You might have written the Fibonacci function as fib n = if n <= 2 then 1 else fib (n-1) + fib (n-2) It can also be written as fib 1 = 1 fib 2 = 1 fib n = fib (n-1) + fib (n-2)

Empty List Pattern You can specify an empty list as a parameter. The definition will be use if it is executed with an empty list mysum :: Num a => [a] -> a mysum [] = 0 mysum str = head str + mysum (tail str )

Haskell Programming Assignment A Haskell programming assignment has been posted on Blackboard You have to write 8 simple Haskell functions Upload one.hs file to Blackboard by noon on Tuesday, April 4, 2017