tupleはlistと似ているが、複数の値を一つの値に保持する
(1, 2)
(1, "Hello")
[(1, 2), (8, 11, 5), (4, 5)]
[(1, 2), ("One", 2)]
tupleが入ったlistを作るとき、以下の場合にエラーがおきる
[("Christopher", "Walken", 55), ("Tom", "York", 50)]
(1)
:t (1)
fst (8, 11)
fst ("Wow", False)
snd (8, 11)
snd ("Wow", False)
zip [1,2,3,4,5] [5,5,5,5,5]
zip [1 .. 5] ["one", "two", "three", "four", "five"]
zip [5,3,2,6,2,7,2,5,4,6,6] ["im","a","turtle"]
zip [1..] ["apple", "orange", "cherry", "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平方の定理を使って直角三角形として成立する組み合わせのみ抽出するフィルターを追加する
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'
最後に、辺の合計が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
plus :: Int -> Int -> Int
plus x y = x + y
:t plus
addOne = plus 1
:t addOne
addOne 2
factorial :: Integer -> Integer
factorial n = product [1..n]
factorial 50
circumference :: Float -> Float
circumference r = 2 * pi * r
circumference 4.0
circumference' :: Double -> Double
circumference' r = 2 * pi * r
circumference' 4.0
()
:t ()
空のtupleは一個の値のみ持つ