-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTuples.hs
36 lines (28 loc) · 1.09 KB
/
Tuples.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module Tuples where
tuple_examples = do
putStrLn ""
putStrLn "tuple_examples"
putStrLn ""
-- Stores list of multiple data types, but has a fixed size
let randTuple = (1, "Random tuple")
putStrLn $ "(1, \"Random tuple\") = " ++ show randTuple
-- A tuple pair stores 2 values
let bobSmith = ("Bob Smith", 52)
putStrLn $ "bobSmith = " ++ show bobSmith
-- Get the first value
let bobsName = fst bobSmith
putStrLn $ "fst bobSmith = " ++ show bobsName
-- Get the second value
let bobsAge = snd bobSmith
putStrLn $ "snd bobSmith = " ++ show bobsAge
-- If we don't care about a field, use '_'
let (_, justBobsAge) = bobSmith
putStrLn $ "justBobsAge = " ++ show justBobsAge
-- zip can combine values into tuple pairs
let names = ["Bob","Mary","Tom"]
let addresses = ["123 Main","234 North","567 South"]
let namesAndAddresses = zip names addresses
putStrLn $ "names = " ++ show names
putStrLn $ "addresses = " ++ show addresses
putStrLn $ "zip names addresses = " ++ show namesAndAddresses
putStrLn "-----------------"