Haskell Refresher Informatics 2D

Similar documents
Informatics 2D. Coursework 1: Search and Games

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

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

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

Functional Logic Programming Language Curry

Logic - CM0845 Introduction to Haskell

Haskell An Introduction

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

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

Standard prelude. Appendix A. A.1 Classes

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18)

CSCE 314 Programming Languages

A general introduction to Functional Programming using Haskell

Shell CSCE 314 TAMU. Higher Order Functions

Programming Paradigms

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

CSCE 314 Programming Languages

Two Dimensional Arrays

Overloading, Type Classes, and Algebraic Datatypes

State Monad (3D) Young Won Lim 9/25/17

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

CSCE 314 Programming Languages

An introduction introduction to functional functional programming programming using usin Haskell

Lecture 4: Higher Order Functions

Lecture 19: Functions, Types and Data Structures in Haskell

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

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

CS 11 Haskell track: lecture 1

Imperative languages

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

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

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

Tic Tac Toe Game! Day 8

CSE 341 Section 5. Winter 2018

CS 340 Spring 2019 Midterm Exam

Higher Order Functions in Haskell

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

List Functions, and Higher-Order Functions

Today in CS161. Lecture #12 Arrays. Learning about arrays. Examples. Graphical. Being able to store more than one item using a variable

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

INTRODUCTION TO HASKELL

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Functional Programming in Haskell for A level teachers

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

Shell CSCE 314 TAMU. Haskell Functions

CS 457/557: Functional Languages

An Introduction to Programming in Haskell. Gabriel J. Ferrer Hendrix College

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

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

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

Introduction to Haskell

Advanced features of Functional Programming (Haskell)

Watch out for the arrows. Recollecting Haskell, Part V. A start on higher types: Mapping, 1. A start on higher types: Mapping, 2.

Monad Background (3A) Young Won Lim 11/18/17

User-Defined Algebraic Data Types

CS 440: Programming Languages and Translators, Spring 2019 Mon

Refactoring to Functional. Hadi Hariri

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

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

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

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Monotonicity. Admissible Search: That finds the shortest path to the Goal. Monotonicity: local admissibility is called MONOTONICITY

Exercise 1 ( = 24 points)

CSCE 314 Programming Languages Functors, Applicatives, and Monads

CS 320: Concepts of Programming Languages

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

Monad Background (3A) Young Won Lim 11/8/17

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Types and Static Type Checking (Introducing Micro-Haskell)

CS 11 Haskell track: lecture 5. This week: State monads

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

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

Advances in Programming Languages

Programming Paradigms

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

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

Introduction to Functional Programming in Haskell 1 / 56

Functional Paradigm II

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

Functional Programming in C++

Exercise 1 ( = 18 points)

Software System Design and Implementation

CSci 450: Org. of Programming Languages Overloading and Type Classes

Logical Methods in... using Haskell Getting Started

Functional Programming in C++

LOGIC PROGRAMMING. Ahmed Sallam. Lecture 4: Backtracking and Cut. Lecture 4: Backtracking and Cut 1

Exercises on ML. Programming Languages. Chanseok Oh

A Sudoku Solver Pruning (3A)

Introduction to Functional Programming and Haskell. Aden Seaman

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

CS 360: Programming Languages Lecture 12: More Haskell

Exercise 1 (2+2+2 points)

Lecture 4: Backtracking and Cut 1. Lecture 4: Backtracking and Cut. Ahmed Sallam

Graphical Untyped Lambda Calculus Interactive Interpreter

CS558 Programming Languages

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Transcription:

Haskell Purely functional! : Everything is a function Haskell Refresher Informatics 2D Kobby. K.A. Nuamah 30 January 2015 Main topics: Recursion Currying Higher-order functions List processing functions such as map, filter, foldl, sortby, etc The Maybe monad More on Haskell: http://www.haskell.org/haskellwiki/haskell Informatics 2D 1 Informatics 2D 2 Types Unlike other programming languages like Java, Haskell has type inference. However, type declarations ensures that you are specific about the input arguments of your function and the output values. next :: Trace -> [Trace] The next function takes an argument of type Trace and returns a list of Traces Type Synonyms type Trace = [(Int,Int)] type Game = [Int] The type Trace is a synonym for a list of (Int,Int) tuples. For code clarity. Informatics 2D 3 Informatics 2D 4 Informatics 2D 1

Recursion Important role in Haskell. Function is recursive when one part of its definition includes the function itself again. Always have a termination condition to avoid infinite loop. length :: [a] -> Int length [] = 0 length (x:xs) = 1 + length xs Currying The process of creating intermediate functions when feeding arguments into a complex function. Note: all functions in Haskell really only take one argument 2 * 3 in Haskell: (*) function takes first argument 2, and return an intermediate function (2*) The new function (2*) takes one argument,3, and completes the multiplication Applying only one parameter to a function that takes two parameters returns a function that takes one parameter Informatics 2D 5 Informatics 2D 6 Higher-Order Functions Functions are just like any other value in Haskell. Functions can take functions as parameters and also return functions. map :: (a -> b) -> [a] -> [b] map _ [] = [] map f ( x: xs ) = f x : map f xs Map takes a function and list and applies that function to every element in the list. List Processing Functions (map, filter, foldl, etc.) map : takes a function and list and applies that function to every element in the list. map :: (a -> b) -> [a] -> [b] filter: takes a predicate (function that returns true or false) and list and then returns the list of all elements that satisfy the predicate. filter:: (a -> Bool) -> [a] -> [a] foldl: takes a binary function, an accumulator and a list. It folds up the items in the list and return a single value. foldl:: (a -> b -> a) -> a -> [b] -> a Informatics 2D 7 Informatics 2D 8 Informatics 2D 2

List Comprehension Build more specific sets out of general sets. to create a list of integers that are multiples of 2 and greater than than 20: [x*2 x <- [1..25], x*2 >= 20] Output function Elements bound to x Condition Or Predicate Maybe Monad The Maybe monad represents computations which might "go wrong" by not returning a value. If a value is returned, it uses Just a, where a is the type of the value. If no value is available, it returns Nothing. safediv::double->double->maybe Double safediv x y y == 0 = Nothing otherwise = Just (x/y) Informatics 2D 9 Informatics 2D 10 Coursework Overview Successor Function Trace type for search problems type Trace = [(Int,Int)] Example : A path from (1,1) to (4,2) [(1,1),(1,2),(2,2),(3,2),(4,2)] 1 2 3 4 5 6 7 8 9 10 1 x x 2 x 3 x 4 x 5 6 7 8 9 The next function returns the possible continuations of the path next::trace->[trace] Example : Suppose we start from are at (4,2) Possible continuations generated by next [[(1,1),(1,2),(2,2),(3,2),(4,2),(4,1)], [(1,1),(1,2),(2,2),(3,2),(4,2),(3,2)], [(1,1),(1,2),(2,2),(3,2),(4,2),(4,3)], [(1,1),(1,2),(2,2),(3,2),(4,2),(5,2)]] 1 2 3 1 2 3 4 5 6 7 8 9 10 4 x 5 6 7 8 9 10 Informatics 2D 11 10 Informatics 2D 12 Informatics 2D 3

Consistency with representation Be consistent with your representation of Traces in Haskell [(1,1),(1,2),(2,2),(3,2),(4,2)] [(4,2),(3,2),(2,2),(1,2),(1,1)] Both are ok, provided you are consistent with the head and tail of your list. Same applies to [Trace] Higher-Order Functions in Coursework Example: bestfirstsearch::(trace -> Bool) -> (Trace ->[Trace]) -> ((Int,Int) -> Int)->[Trace] -> Maybe Trace (Trace Bool) is the type of the goal function (same as uninformed search). (Trace [Trace]) is the type of the next function (same as uninformed search). ((Int,Int) Int) is the type of the heuristic function, which defines at least an ordering on the nodes in the search agenda. [Trace] is the search agenda (same as uninformed search). Maybe Trace is the value the function returns (same as uninformed search ). Informatics 2D 13 Informatics 2D 14 Game (Tic-Tac-Toe) Representation Game Representation Examples Game represented as a list of Integers type Game = [Int] A new game will be represented as [-1,-1,-1,-1,-1,-1,-1,-1,-1] Max player is represented by a 1 in the list. Min player is represented as 0 in the list. An unplayed cell is represented as -1 X O X O X O O O X New Game: [-1,-1,-1,-1,-1,-1,-1,-1,-1] Min Move: [-1,-1,-1,-1, 0,-1,-1,-1,-1] O Types for Cell and Player type Player = Int type Cell = (Int,Int) Max Move: [ 1,-1,-1,-1, 0,-1,-1,-1,-1] X O Informatics 2D 15 Informatics 2D 16 Informatics 2D 4

Lines in Game The Line type represents any of the lines on the game board: rows, columns and diagonals. type Line = [Int] Examples of Lines for the game state given: Row 1: [1,0,1] Row 3: [0,0,1] Column 1: [1,0,0] Diagonal 1: [1,1,1] To get all lines for a game state, use function: getlines::game->[line] X O X O X O O O X Other useful functions maxplayer function checks if the given player is max, and returns a Boolean. maxplayer::player->bool switch function alternates between players. switch::player->player terminal function checks if the game argument is in a terminal state. terminal::game->bool ismovevalid checks if a move made in a given game state is a valid one for a given player. ismovevalid::game->player->cell->bool playmove makes a move to a cell and returns the new game state. This functions is called for human player moves. playmove::game->player->cell->game moves function returns a list of possible moves/successor states that a player can make given a game state. moves::game->player->[game] checkwin function checks if the game state is a win for the player argument. checkwin::game->player->bool Informatics 2D 17 Informatics 2D 18 Informatics 2D 5