tupleはlistと似ているが、複数の値を一つの値に保持する
(1, 2)
(1, "Hello")
(1,2)
(1,"Hello")
[(1, 2), (8, 11, 5), (4, 5)]
[(1, 2), ("One", 2)]
tupleが入ったlistを作るとき、以下の場合にエラーがおきる
[("Christopher", "Walken", 55), ("Tom", "York", 50)]
[("Christopher","Walken",55),("Tom","York",50)]
(1)
:t (1)
1
fst (8, 11)
fst ("Wow", False)
8
"Wow"
snd (8, 11)
snd ("Wow", False)
11
False
zip [1,2,3,4,5] [5,5,5,5,5]
[(1,5),(2,5),(3,5),(4,5),(5,5)]
zip [1 .. 5] ["one", "two", "three", "four", "five"]
[(1,"one"),(2,"two"),(3,"three"),(4,"four"),(5,"five")]
zip [5,3,2,6,2,7,2,5,4,6,6] ["im","a","turtle"]
[(5,"im"),(3,"a"),(2,"turtle")]
zip [1..] ["apple", "orange", "cherry", "mango"]
[(1,"apple"),(2,"orange"),(3,"cherry"),(4,"mango")]
let triangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10] ]
let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2]
rightTriangles
[(3,4,5),(6,8,10)]
続いて、3平方の定理を使って直角三角形として成立する組み合わせのみ抽出するフィルターを追加する
let rightTriangles' = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2, a+b+c == 24]
rightTriangles'
[(6,8,10)]
最後に、辺の合計が24である組み合わせのみ抽出するフィルターを追加する
:t 'a'
:t True
:t "Hello!"
:t (True, 'a')
:t 4 == 5
:t
コマンドで式の型を知ることができる:t
コマンドに渡した式 ::
型」 の形式で表示されるremoveNonUppercase :: [Char] -> [Char]
removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']]
removeNonUppercase :: [Char] -> [Char]
removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']]
addThree :: Int -> Int -> Int -> Int
addThree x y z = x + y + z
addThree 1 2 3
6
plus :: Int -> Int -> Int
plus x y = x + y
:t plus
addOne = plus 1
:t addOne
addOne 2
3
factorial :: Integer -> Integer
factorial n = product [1..n]
factorial 50
30414093201713378043612608166064768844377641568960512000000000000
circumference :: Float -> Float
circumference r = 2 * pi * r
circumference 4.0
25.132742
circumference' :: Double -> Double
circumference' r = 2 * pi * r
circumference' 4.0
25.132741228718345
()
:t ()
()
空のtupleは一個の値のみ持つ