Computer Science 312 Haskell Lists 1.

Slides:



Advertisements
Similar presentations
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Advertisements

Haskell Lets review some of the Haskell concepts you have been learning on your own. The answers are included, but try it yourself first.
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can.
Exercises – don’t use built in functions for these as we want the practice Write a recursive function to add up all the numbers in a list "flatten" a list.
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Functional Programming Notes for CSCE 190 Based on Sebesta,
Tuples and Lists Lecture 3, Programmeringsteknik del A.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
Operators, Functions and Modules1 Pattern Matching & Recursion.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
1 Functional Programming Lecture 6 - Algebraic Data Types.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Overview of the Haskell 98 Programming Language
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
0 INTRODUCTION TO FUNCTIONAL PROGRAMMING Graham Hutton University of Nottingham.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
Chapter SevenModern Programming Languages1 A Second Look At ML.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
Computer Science 101 Python Lists. Literals, Assignment, Comparisons, Concatenation Similar to the behavior of strings so far a = [1, 2, 3] b = range(1,
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Advanced Functional Programming 2010
Lecture 14: Advanced Topic: Functional Programming
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Functional Programming
Conditional Expressions
Introduction to Lists (background for lab 1)
PROGRAMMING IN HASKELL
Haskell Chapter 2.
Functions and patterns
A lightening tour in 45 minutes
Haskell Chapter 4.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
Haskell Strings and Tuples
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
Records and Type Classes
Functions and patterns
CSCE 314: Programming Languages Dr. Dylan Shell
Defining New Data Types
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher-Order Functions in Haskell
PROGRAMMING IN HASKELL
Lisp.
PROGRAMMING IN HASKELL
Records and Type Classes
Presentation transcript:

Computer Science 312 Haskell Lists 1

List Literals Similar to Python, but all items must be of the same type Prelude> [] [] Prelude> [1, 2, 3] [1,2,3] Prelude> [1..10] {1,2,3,4,5,6,7,8,9,10]

Basic List Operations null list Returns true if list is empty or false otherwise. head list Returns the first item (at position 0) in list. tail list Returns a list of the items after the first item in list. length list Returns the number of items in list. item : list Returns a list whose head is item and whose tail is list. list1 ++ list2 Returns a list containing the items in list1 followed by the items in list2. list !! index Returns the item at position index in list. Positions are counted from 0. Many other list operations are in the Data.List module

The Essential Operations With these four operations, you can build almost all of the others Prelude> numbers = [ 1..4] Prelude> head numbers 1 Prelude> tail numbers [2,3,4] Prelude> 0:numbers [0,1,2,3,4] Prelude> numbers [1,2,3,4]

The Essential Operations With these four operations, you can build almost all of the others Prelude> :type [1,2,3,4] [1,2,3,4] :: Num t => [t] Prelude> :type head head :: [a] -> a Prelude> :type tail Tail :: {a] -> [a] Prelude> :type (:) (:) :: a -> [a] -> [a] head and tail are selectors, and (:) is a constructor

A Recursive Definition of a List A list is either empty (that is, null is true) or an item (called the head) followed by another list (called the tail)

Recursive List Processing: Length The length of a list is 0 if the list is empty 1 + the length of the tail of the list, otherwise myLength :: [a] -> Int myLength list | null list = 0 | otherwise = 1 + myLength (tail list)

Recursive List Processing: ith Item The ith item of a list is The list’s head if i = 0 The ith item at i - 1 in the list’s tail, otherwise -- Assumes that list is nonempty! ith :: [a] -> Int -> a ith list i | i == 0 = head list | otherwise = ith (tail list) (i - 1)

Recursive List Processing: remove ith The remove ith of a list is The list’s tail if i = 0 The list’s head, followed by the remove ith of the list’s tail at i - 1 -- Assumes that list is nonempty! removeIth :: Int -> [a] -> [a] removeIth index list | index == 0 = tail list | otherwise = head list : removeIth (index – 1) (tail list) Adds head of list to result of recursive call

Trace of Remove ith The remove ith of a list is The list’s tail if i = 0 The list’s head, followed by the remove ith of the list’s tail at i - 1 removeIth 2 [20, 30, 40, 50] -> -- index /= 0 20 : removeIth 1 [30, 40, 50] -> -- index /= 0 30 : removeIth 0 [40, 50] -> -- index == 0, tail == [50] <- [50] -- Returns [50] <- [30, 50] -- Returns 30:[50] <- [20, 30, 50] -- Returns 20:[30, 50]

Using (:) for Pattern Matching Prelude> 1:[2,3,4] --Use (:) as constructor [1,2,3,4] Prelude> (x:xs) = [1,2,3,4] -- Use (:) as selector Prelude> x 1 Prelude> xs [2,3,4] x extracts the head, and xs extracts the tail

Pattern Matching with Lists myLength :: [a] -> Int myLength list | null list = 0 | otherwise = 1 + myLength (tail list) myLength :: [a] -> Int myLength [] = 0 myLength (x:xs) = 1 + myLength xs Back to pure clausal form for list processing functions!

Pattern Matching with Lists -- Assumes that list is nonempty! removeIth :: Int -> [a] -> [a] removeIth index list | index == 0 = tail list | otherwise = head list : removeIth (index – 1) (tail list) -- Assumes that list is nonempty! removeIth :: Int -> [a] -> [a] removeIth 0 (x:xs) = xs removeIth i (x:xs) = x : removeIth (index – 1) xs

To Mutate or Not to Mutate # Python list >>> lyst = [1,2,3,4] >>> lyst.pop(1) 2 >>> lyst [1,3,4] -- Haskell list Prelude> list = [1,2,3,4] Prelude> removeIth 1 list [1,3,4] Prelude> list [1,2,3,4] removeIth is a pure function, which transforms a list into another list

No Mutations, Can Share Structure -- Assumes that list is nonempty! removeIth :: Int -> [a] -> [a] removeIth 0 (x:xs) = xs removeIth i (x:xs) = x : removeIth (index – 1) xs Prelude> list = [1,2,3,4] Prelude> newList = removeIth 1 list Prelude> newList [1,3,4] Prelude> list [1,2,3,4] list 1 2 3 4 newList 1

Data.List.Sort Prelude> import Data.List (sort) Prelude> :type sort sort :: Ord a => [a] -> [a] Prelude> list = [4,3,5,1] Prelude> sort list [1,3,4,5] Prelude> list [4,3,5,1]

Exercise Define a function insertIth. This function expects an integer index, a new item, and a list as arguments, and returns a list in which the new item is now at position i. Assume i >= 0 && < length list. -- Usage Prelude> list = [1,2,3,4] Prelude> insertIth 0 99 list [99,1,2,3,4] Prelude> insertIth 2 34 list [1,2,99,3,4]

Index-Based vs Value-Based Functions Prelude> list = [1,2,3,3,4] Prelude> removeIth 2 list [1,2,3,4] Prelude> removeOne 2 list [1,3,3,4] Prelude> removeAll 3 list [1,2,4] removeOne :: Eq a => a -> [a] -> [a] removeOne item list removeAll :: Eq a => a -> [a] -> [a] removeAll item list All instances of type class Eq implement the == and != operators

Value-Based Functions Prelude> removeOne 2 list [1,3,3,4] removeOne :: Eq a => a -> [a] -> [a] removeOne item [] = [] removeOne item (x:xs) | item == x = xs | otherwise = x : removeOne item xs

Introduction to strings and tuples For next time Introduction to strings and tuples