Open5

すごいHaskellたのしく学ぼう!を読んだメモ

もとあきもとあき

リスト内包表記、めっちゃいいな

rightTriangles' = [ (a,b,c) | c <- [1..10], a <- [1..c], b <- [1..a], a^2 + b^2 == c^2, a+b+c == 24]
もとあきもとあき

柔軟

src/Pipeline.hs
module Pipeline ((|>)) where

infixl 1 |>
(|>) a f = f a
ghci> print $ [1..] |> map (^2) |> take 50
[1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400,441,484,529,576,625,676,729,784,841,900,961,1024,1089,1156,1225,1296,1369,1444,1521,1600,1681,1764,1849,1936,2025,2116,2209,2304,2401,2500]

参考文献
https://tex2e.github.io/blog/programming/haskell-pipeline-operator

もとあきもとあき

レコード構文でデータ型を定義すると、2つの方法で値を作れる

data Car = Car
  { company :: String,
    model :: String,
    year :: Int
  }
  deriving (Show)
ghci
ghci> print Car {company = "Toyota", model = "Prius", year = 2017}
Car {company = "Toyota", model = "Prius", year = 2017}
ghci> print $ Car "Toyota" "Prius" 2017
Car {company = "Toyota", model = "Prius", year = 2017}