HASKELL MODULES INPUT and OUTPUT 2 Modules What

  • Slides: 44
Download presentation
HASKELL (++) “MODULES. ” “ ” ++ “INPUT and OUTPUT

HASKELL (++) “MODULES. ” “ ” ++ “INPUT and OUTPUT

2

2

Modules What is Module? 3

Modules What is Module? 3

Modules 4

Modules 4

Modules 5

Modules 5

What is Modules? → A Haskell module is a collection of related functions, types

What is Modules? → A Haskell module is a collection of related functions, types and typeclasses. 6

Modules The Haskell standard library is split into modules, each of them contains functions

Modules The Haskell standard library is split into modules, each of them contains functions and types that are somehow related and serve some common purpose. There’s a module for manipulating lists, a module for concurrent programming, a module for dealing with complex numbers, etc. 7

Modules 8

Modules 8

Modules ● How to import Modules? → import <module name> Manipulating List Data. List

Modules ● How to import Modules? → import <module name> Manipulating List Data. List 9

Modules Example : import Data. List unique. Num: : (Eq a) => [a] ->Int

Modules Example : import Data. List unique. Num: : (Eq a) => [a] ->Int unique. Num = length. nub Here, nub is a function that belongs to Data. List module that take a list and weeds out all duplicate entry. → all Functions that Data. List export become available in the global namespace. 10

Modules ● Several Module can be load at a time. ○ m + Data.

Modules ● Several Module can be load at a time. ○ m + Data. List Data. Map Data. Set ● Only couple of function from module can be imported ○ import Data. List ( nub, sort) ● All functions of a module except selected one ○ import Data. List hiding( nub) ● Data. Map : offers a data structure for looking up values 11

Modules Data. List > Different Functions 12

Modules Data. List > Different Functions 12

Modules Data. List > Intersperse Take an element and a list Then put that

Modules Data. List > Intersperse Take an element and a list Then put that element in between each pair of elements in the list → Example : intersperse '. ' "RAJESH" Intercalate Take a list of list Return a list 13

Modules Data. List > Transpose It transpose a list of list. ? ? →

Modules Data. List > Transpose It transpose a list of list. ? ? → Matrix Transpose. !! Example : transpose [[1, 2, 3], [4, 5, 6], [7, 8, 9]] 14

Modules Data. List > concat flattens a list of lists into just a list

Modules Data. List > concat flattens a list of lists into just a list of elements. Example : concat ["Guys", " I am sorry", " I am making you", " bore"] concat. Map Same a mapping function Take a list and concat then Example : concat. Map (replicate 4) ['a'. . 'f'] → "aaaabbbbccccddddeeeeffff" 15

Modules Data. List > ● and ○ And takes a list of boolean values

Modules Data. List > ● and ○ And takes a list of boolean values ○ Return True ‘or’ False Example : and $map (>4) [5, 6, 7, 8, 9] ● or ○ Please guys don’t ask me what OR do. : x. D Example : or $map (==4) [4, 5, 6, 7, 8, 9] 16

Modules Data. List > ● any AND all ○ Take a predicate ○ Check

Modules Data. List > ● any AND all ○ Take a predicate ○ Check any or all the elements in a list satisfy the predicate. Example : any (==4) [1, 2, 3, 4, 5, 6, 7] all (`elem` ['A'. . 'Z']) "I am So SORRY" ● iterate ○ Take a function and a starting value ○ It applies the function on to the value, then result, then again ○ Then again……. . : ) Example : take 5 $ iterate (++ "Haha") "Lo. L" 17

Modules Data. List > ● split. At ○ Take a number and List ○

Modules Data. List > ● split. At ○ Take a number and List ○ Then split the list and return two lists in a tuple Example : split. At 3 "Hey. Guys" NB : if it does not find the index then return empty tuple with input list ● find ○ Take a list and a predicate ○ Return the first element that satisfies the predicate Example : find (>5) [1, 2, -1, -2, 5, 23, 25, 26, 28] 18

Modules Data. List > ● take. While ○ Takes elements from the list while

Modules Data. List > ● take. While ○ Takes elements from the list while the predicate holds. ○ When it encountered one element that does not satisfy the predicate it cut off. Example : take. While (>12) [3, 4, 6, 7, 18, 19, 13, 12, 15] take. While (>12) [18, 19, 13, 12, 15] 19

Modules Data. List > ● drop. While ○ Drop all the elements while the

Modules Data. List > ● drop. While ○ Drop all the elements while the predicate is true. Example : drop. While (/=' ') "Iam Sorry for the boring Lecture" drop. While (<5) [1, 2, 3, 4, 5, 6, 7, 8] 20

Modules Data. List > ● span ○ Is kind of like take. While. But

Modules Data. List > ● span ○ Is kind of like take. While. But return a pair of list ○ First list contains everything the resulting list from take. While ○ Second list contains rest of the input data. Example : let (f. Part, l. Part) = span (/=' ') "Boring Lecture going on" in "First Part : " ++ f. Part ++ ", Last Part : " ++ l. Part 21

Modules Data. List > ● break ○ Break the list when the predicate is

Modules Data. List > ● break ○ Break the list when the predicate is true for the first time Example : break (==5) [1, 2, 3, 4, 6, 5, 6, 7, 8, 9] ● group ○ Take a list and group adjacent elements into sublist if they are equal Example : group [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 2, 2, 2, 6, 6] 22

Modules Data. List > ● group→ ○ If we sort a list before group

Modules Data. List > ● group→ ○ If we sort a list before group it then we can find how many times each element appears in the list. Example : map (l@(x: xs) -> (x, length l)). group. sort $ [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 2, 2, 2, 6, 6] ● partition ○ Take a list and a predicate ○ Return pair of list Example : partition (`elem` ['A'. . 'Z'] ) "Sorry For The Lengthy Lecture"23

Modules Data. List > ● elem. Index ○ Return the index of element ○

Modules Data. List > ● elem. Index ○ Return the index of element ○ Index start from 0 Example : 4 `elem. Index` [1, 2, 3, 4, 5, 6] ● elem. Indices ○ Return the list of indices, in case the element we are looking for crops up in our list several times. ○ Failure will return empty list Example : ' ' `elem. Indices` "Where are the spaces? " 24

Modules Data. List > ● zip 3, zip 4 etc. And zip. With 3,

Modules Data. List > ● zip 3, zip 4 etc. And zip. With 3, zip. With 4 ○ Allows to zip X number of lists and to zip X amount of lists with X amount of parameters. Example : zip. With 3 (x y z -> x + y + z) [1, 2, 3, 4] [5, 6, 7, 8] [9, 10, 11, 12] zip 4 [2, 3, 3] [2, 2, 2] [5, 5, 3] [2, 2, 2] 25

Modules Data. List > ● words and unwords ○ For splitting a line of

Modules Data. List > ● words and unwords ○ For splitting a line of text or joining a list of text into a text Example : words "I know it's tough to get your concentration nothing is working" Unwords ["I", "know", "it's", "tough", "to", "get", "your", "concentration", "nothing", "is", " working"] 26

Modules Data. List > ●  ○ It removes matching elements from left side

Modules Data. List > ● ○ It removes matching elements from left side list. !! Example : "I am a boring presenter" \ "boring" ● Intersect ○ Return only the elements that are found in both list Example : [1. . 7] `intersect` [5. . 10] 27

Modules Data. Char > ● Doing (==) ‘on’ (>0) returns an equality function that

Modules Data. Char > ● Doing (==) ‘on’ (>0) returns an equality function that look like ○ xy -> (x>0) == (y>0) group. By ((==) `on` (> 0)) values [[ -4. 3 , -2. 4 , -1. 2] , [0. 4 , 2. 3 , 5. 9 , 10. 5 , 29. 1 , 5. 3] , [ -2. 4 , -14. 5] , [2. 9 , 2. 3]] ● sort. By ○ Can sort the list based on their length. Example : let xs = [[5, 4, 4], [1, 2, 3], [3, 5, 4, 3], [2], [2, 2]] sort. By (compare `on` length) xs 28

Modules Data. Char > The Data. Char module does what its name suggests. It

Modules Data. Char > The Data. Char module does what its name suggests. It exports functions that deal with characters. It’s also helpful when filtering and mapping over strings because they’re just lists of characters. ● is. Control ○ Check whether a character is a control character. ● is. Space ○ Check whether a character is a white-space characters. That 29 includes spaces, tab, characters, newline etc.

Modules Data. Char > ● is. Lower ○ Check whether a character is lowercase.

Modules Data. Char > ● is. Lower ○ Check whether a character is lowercase. ● is. Upper ○ Check whether a character is uppercase. ● is. Alpha ○ Check whether a character is a letter. ● is. Alpha. Num ○ Check whether a character is a letter or a number. 30

Modules Data. Char > ● is. Print ○ Check whether a character is printable.

Modules Data. Char > ● is. Print ○ Check whether a character is printable. ● is. Digit ○ Check whether a character is a digit. ● is. Oct. Digit ○ Check whether a character is an octal digit. ● is. Hex. Digit ○ Check whether a character is a hex digit. 31

Modules Data. Char > ● is. Letter ○ Check whether a character is a

Modules Data. Char > ● is. Letter ○ Check whether a character is a letter. ● is. Symbol ○ Check whether a character is a fancy mathematical or currency symbol. ● is. Number ○ Check whether a character is numeric. 32

Modules Data. Map > from. List function takes an association list (in the form

Modules Data. Map > from. List function takes an association list (in the form of a list) and returns a map with the same associations. Map. from. List [(1 , 2) , (3 , 4) , (3 , 2) , (5 , 5)] NB: import qualified Data. Map as Map Example : phone. Book = [("Rajesh", "+48 605 122 880"), ("Arash", "+48 575 522 928"), ("Marek", "+48 575 932 928"), ("Lucille", "+48 575 052 928") , ("wendy", "+48 575 398 282"), ("John", "+48 575 532 492") ] 33

Modules Data. Map > find. Phone. Number : : (Eq k) => k ->

Modules Data. Map > find. Phone. Number : : (Eq k) => k -> [(k, v)] -> v find. Phone. Number key xs = snd. head. filter ((k, v) -> key == k) $ xs ● Map. empty ● Map. insert 3 100 Map. empty ● Map. null $ Map. from. List [(2, 3), (5, 5)] 34

Modules Data. Map > ● Map. size $ Map. from. List [(2, 4), (3,

Modules Data. Map > ● Map. size $ Map. from. List [(2, 4), (3, 3), (4, 2), (5, 4), (6, 4)] ● singelton ○ Takes a key and value and creates a map that has exactly one mapping Example : Map. singleton 3 9 Map. insert 5 9 $ Map. singleton 3 9 ● member ○ Is a predicate takes a key and a map and reports whether the 35

Modules Data. Map > Example : Map. member 3 $ Map. from. List [(3,

Modules Data. Map > Example : Map. member 3 $ Map. from. List [(3, 6), (4, 3), (6, 9)] Map. member 5 $ Map. from. List [(3, 6), (4, 3), (6, 9)] 36

Modules Data. set > The Data. Set module offers us, well, sets. Like sets

Modules Data. set > The Data. Set module offers us, well, sets. Like sets from mathematics. Sets are kind of like a cross between lists and maps. All the elements in a set are unique. And because they’re internally implemented with trees (much like maps in Data. Map), they’re ordered. Checking for membership, inserting, deleting, etc. is much faster than doing the same thing with lists. NB : import qualified Data. Set as Set 37

Modules Data. set > ● from. List ○ Function works much like you would

Modules Data. set > ● from. List ○ Function works much like you would expect. It takes a list and convert it into set ○ Then we can run operation as like we can do in set 38

Modules Data. set > Example : text 1 = "I am not going tomorrow"

Modules Data. set > Example : text 1 = "I am not going tomorrow" text 2 = "I am going I have no choice" let set 1 = Set. from. List text 1 let set 2 = Set. from. List text 2 Set. intersection set 1 set 2 Set. difference set 1 set 2 39

Modules 40

Modules 40

Modules 41

Modules 41

Modules 42

Modules 42

43

43

Modules 44

Modules 44