らんだむな記憶

blogというものを体験してみようか!的なー

はぢめてのHaskell(6)

ghciの中で1行で定義してみる。まずは、結合の関係からまずいであろうパターン。

Prelude> let prime x i = if i >= x then True else if x `mod` i == 0 then False else prime x i+1

<interactive>:13:85:
    Could not deduce (Num Bool) arising from a use of `+'
    from the context (Integral a)
      bound by the inferred type of prime :: Integral a => a -> a -> Bool
      at <interactive>:13:5-86
    Possible fix: add an instance declaration for (Num Bool)
    In the expression: prime x i + 1
    In the expression: if x `mod` i == 0 then False else prime x i + 1
    In the expression:
      if i >= x then
          True
      else
          if x `mod` i == 0 then False else prime x i + 1

ほらね。次にこれならいけるだろ?ってやつ:

Prelude> let prime x i = if i >= x then True else if x `mod` i == 0 then False else prime x (i+1)
Prelude> prime 97 2
True

OK!!