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

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

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Haskell Types COMP360

A general introduction to Functional Programming using Haskell

Haskell An Introduction

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

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Haskell Scripts. Yan Huang

Standard prelude. Appendix A. A.1 Classes

Lecture 19: Functions, Types and Data Structures in Haskell

Programming Languages Fall 2013

CS 360: Programming Languages Lecture 10: Introduction to Haskell

CS 320: Concepts of Programming Languages

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

Functional Programming for Logicians - Lecture 1

Introduction to Haskell

CS 360: Programming Languages Lecture 12: More Haskell

CS 457/557: Functional Languages

CSc 372 Comparative Programming Languages

CIS 194: Homework 4. Due Wednesday, February 18, What is a Number?

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

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

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

Programming in Haskell Aug-Nov 2015

Functional Programming in Haskell Part I : Basics

CS 11 Haskell track: lecture 1

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

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Functional Programming and Haskell

CSC324 Principles of Programming Languages

Course year Typeclasses and their instances

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Programming Paradigms

Lecture 4: Higher Order Functions

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell:

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

List Functions, and Higher-Order Functions

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

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

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

Exercises on ML. Programming Languages. Chanseok Oh

INTRODUCTION TO HASKELL

Programming in Haskell Aug Nov 2015

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

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

Overloading, Type Classes, and Algebraic Datatypes

An introduction introduction to functional functional programming programming using usin Haskell

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Lecture 2: List algorithms using recursion and list comprehensions

Introduction to Programming, Aug-Dec 2006

Haskell Making Our Own Types and Typeclasses

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

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

CSE399: Advanced Programming. Handout 2

Haskell Syntax in Functions

Lecture 1 August 9, 2017

Advances in Programming Languages

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

Intro to Haskell Notes: Part 5

Topic 1: Introduction

Haskell Types COMP360

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

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

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Type Processing by Constraint Reasoning

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

First Haskell Exercises

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

Introduction to Programming: Lecture 10

Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes. Haskell Overloading (3) Haskell Overloading (2)

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

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Logical Methods in... using Haskell Getting Started

Logic - CM0845 Introduction to Haskell

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Polymorphism Overview (1A) Young Won Lim 2/20/18

Shell CSCE 314 TAMU. Functions continued

Lists. Michael P. Fourman. February 2, 2010

CSCE 314 Programming Languages

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

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Monads seen so far: IO vs Gen

Programming Paradigms

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

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

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

CIS 194: Homework 5. Due Friday, October 3, Rings. No template file is provided for this homework. Download the

Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November Type Classes. Don Sannella University of Edinburgh

Functional Programming in Haskell for A level teachers

SML A F unctional Functional Language Language Lecture 19

Type-indexed functions in Generic Haskell

Week 5 Tutorial Structural Induction

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

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

CS 320: Concepts of Programming Languages

CSCE 314 Programming Languages

Transcription:

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 code (.lhs extension). If you are looking at the PDF version, you should download the.lhs file, which can be loaded into ghci or compiled with ghc. This document uses Markdown syntax, compiled to PDF using Pandoc. Everything that is not enclosed in a code block like below is treated as text. -- This is code main :: IO () main = undefined Exercise 1 - Types of values ['a', 'b', 'c'] Is a list of characters [Char] ('a', 'b', 'c') Is a 3-Tuple of characters (Char, Char, Char) [(False, '0'), (True, '1')] Is a list of (Bool, Char) tuples. Char)] [(Bool, ([False, '0'], [True, '1']) Is not a valid type, as lists cannot contain elements of different types. [tail, init, reverse] Is a list of functions which take a list of some type a, and returns another list of same type. [[a] -> [a]] Note: Function types are defined by their parameter type(s) and return value type, enclosed by parenthesis. As such, a function with N parameters would have the type: (arg1type -> arg2type ->... -> argntype -> restype) Exercise 2 - Constructors To create the list ['a', 'b', 'c'] explicitly with constructors, we would write 'a':'b':'c':[]. Haskell does allow and understand ['a', 'b', 'c'], but that is just syntactic sugar, and behind the scenes the explicit version is used. ( ) 'a' 'b' 'c' Will construct the 3-Tuple ('a', 'b', 'c') Note: A 2-Tuple would use single comma (,) a b, 4-Tuple would use 3 commas (,) a b c d and so on. The two can be combined: (,) False '0' : (,) True '1' : [], and we get the list [(False, '0'), (True, '1')] 1

Finally, we can also construct a list of functions this way: -- Loadable in ghci functionlist = tail : init : reverse : [] Notice how calling functionlist in ghci gives an error, this is because it has not been told how to Show these functions, only their results. You can however still use the functions for their intended use, from the list. Exercise 3 - Function types All functions will be loadable into ghci with the.lhs document. The function second returns the second element of a list by getting the head (first element) of the tail (all but the first element). This will work of any list type, as such we define the type as [a] -> a. In general, if a type can be anything, the letters a, b, c... are used in order, as needed. second :: [a] -> a -- The type declaration second xs = head (tail xs) The function swap swaps the order of elements in a 2-Tuple, as such we can define the type as (a, b) -> (b, a) Note the usage of a and b. We use two different letters as we cannot guarantee that the first and second element of the tuple is of same type. swap :: (a, b) -> (b, a) swap (x, y) = (y, x) The function pair takes two values and returns a tuple of the two values, somewhat similarly to previous, we can define the type as a -> b -> (a, b). Again, we do not know that x and y will be of same type, Additionally, notice the arrow between a and b, this is because we no longer get a tuple, but two arguments instead. pair :: a -> b -> (a, b) pair x y = (x, y) The function double simply multiplies a number by 2. That does however mean that we need to limit the type to a number. A naive option would be allowing only integer values: Int -> Int. However, a much better option is available: Type constraints. These constraints will allow any type belonging to a given typeclass (e.g. Num for number). This allows us to define the function type Num a => a -> a. double :: Num a => a -> a double x = x*2 The function palindrome takes a list and performs an equivalence test on its reverse. As such, we can define the type as Eq a => [a] -> Bool. The Eq 2

typeclass constraint is added as we need to make sure it is possible to do equality comparison on the elements of the given list. palindrome :: Eq a => [a] -> Bool palindrome xs = reverse xs == xs The function twice applies a function f twice. One should note that the return type of f has to be the same as the argument type. We write the function type as (t -> t) -> t -> t. twice :: (t -> t) -> t -> t twice f x = f (f x) Exercise 4 - Types and functionality (:) 'a' is a function [Char] -> [Char] which appends an a to a list. (,) 5 is a function b -> (Int, b) which constructs a 2-Tuple with first element being the number 5, and second element the argument given. (+) 2 is a function Num a => a -> a which adds 2 to the number provided as argument. Exercise 5 - orelse implementation Two examples using pattern matching only. orelse :: Maybe t -> t -> t orelse (Just e) _ = e orelse _ f = f Another option: orelse_ :: Maybe t -> t -> t orelse_ Nothing f = f orelse_ (Just e) _ = e Exercise 6 - Function examples This exercise is one that you really should play around with yourself. f :: Num a => (a->a) -> a f func = func (func 1) -- same as twice except always called with 1. g :: Num a => a -> (a->a) g x y = x*x + y*y -- adds squares of x and y 3

h :: Num a => (a->a) -> (a->a) h func x = x + func 42 -- adds x to func called with 42 Exercise 7 - sign implementation While possible (As seen below), Creating such a function with only the typeclass Num, is difficult to a point where neither I or Søren have been able to do it nicely. As sign is already implemented as signum for all instances of Num, we can simply: sign :: Num a => a -> a sign = signum -- Not very interesting option An implementation using strictly the Num typeclass seems like a lot of work, instead we include the Ord typeclass so we can use <, = and >. signord :: (Num a, Ord a) => a -> a -- If-then-else signord 0 = 0 signord x = if x < 0 then -1 else 1 Another one signord' :: (Num a, Ord a) => a -> a -- Guards, will be covered later signord' x x == 0 = 0 x < 0 = -1 x > 0 = 1 If you are interested in the use of guards, I suggest reading the chapter on Syntax in functions of Learn you a Haskell for great good! Some versions using other typeclasses signintegral :: Integral a => a -> a signintegral 0 = 0 signintegral x = x `div` abs x signfrac :: (Fractional a, Eq a) => a -> a signfrac 0 = 0 signfrac x = x / abs x 4

Exercise 8 - More types one x = 1 could have the type t -> Int, ghc generalises to one :: Num t => t1 -> t apply f x = f x could have the type (a -> b) -> a -> b ghc: apply :: (t1 -> t) -> t1 -> t compose f g x = f (g x) could have the type (b -> c) -> (a -> b) -> a -> c, as g is applied before f. ghc writes this slightly differently: compose :: (t2 -> t1) -> (t -> t2) -> t -> t1 5