学習ログ

Local Variables

-- foo.hs
foo x = let a = 0
            b = 1
            c = 2
            d = map (* 2) [1..]   
        in if x >= 3
            then d
            else [a, b, c]
>:load foo.hs
...
> foo 2
[0,1,2]
> foo 3
[0,1,2]
[2,4,6,8,10,12,14,16,18,20,.....
-- bar.hs
bar x = if x >= 3
        then d
        else [a, b, c]
  where a = 0
        b = 1
        c = 2
        d = map (* 2) [1..]

whereを使う方が、本文の式を強調できる。スペースの幅に注意。

Real World Haskell: Code You Can Believe In

Real World Haskell: Code You Can Believe In