scrap

Haskellを学んでいる時のあれこれ

Haskellを学んだときのあれこれの雑書きと後で調べるようのメモ

4項目 ·

対話モードで複数行入力する方法

ghci> :{
ghci| lucky :: (Integral a) => a -> String
ghci| lucky 7 = "LUCKY NUMBER SEVEN!"
ghci| lucky x = "Sorry, you're out of luck, pal!"
ghci| :}
ghci> lucky 7
"LUCKY NUMBER SEVEN!"
ghci> lucky 8
"Sorry, you're out of luck, pal!"

ガード節のその他

haskell では、Rust と違って網羅する必要がなく、その他は otherwise でまとめられる Rust でいう _ のようなもの

bmiTell :: (RealFloat a) => a -> String
bmiTell bmi
  | bmi <= 18.5 = "You're underweight, you emo, you!"
  | bmi <= 25.0 = "You're supposedly normal. Pffft, I bet you're ugly!"
  | bmi <= 30.0 = "You're fat! Lose some weight, fatty!"
  | otherwise   = "You're a whale, congratulations!"

BindingとAssignment

Notebook LMに、
4 Declarations and Bindings を元に掲題についての回答

  • 宣言の順序が不問
  • 数学的な等価性
  • パターンマッチングとの統合
  • 多相性(ポリモーフィズム)の付与
  • 単態性制限 (Monomorphism Restriction)

んーよくわからん

Where と Let の違い

  • Where
cylinder :: (RealFloat a) => a -> a -> a
cylinder r h = sideArea + 2 * topArea
    where sideArea = 2 * pi * r * h
          topArea = pi * r^2
  • Let
cylinder :: (RealFloat a) => a -> a -> a  
cylinder r h = 
    let sideArea = 2 * pi * r * h  
        topArea = pi * r ^2  
    in  sideArea + 2 * topArea