where節。
where節以降で関数を定義すると、その直前で定義した関数からのみ使える関数が定義関数を定義、変数を束縛できる。
main = do print $ whereTest 1 whereTest :: Int -> Int whereTest n = plus 5 where plus :: Int -> Int plus num = n + num
この場合、plus関数はwhereTest関数からのみ使用可能。また、plus関数からはwhereTest関数の引数を参照できる。
main = do print $ whereTest 1 print $ plus 5
としてコンパイルすると、
Not in scope: `plus'
怒られる。
where節以降でmain関数からも使用する関数を定義する場合は、
whereTest :: Int -> Int whereTest n = plus 5 where plus :: Int -> Int plus num = n + num minus :: Int -> Int minus n = n - 1
インデントをwhere節の外にして定義する。