[Write up local functions. Bryan O'Sullivan **20070919205327] { hunk ./en/ch03-funcs-types.xml 869 + A few observations about complex numbers + hunk ./en/ch03-funcs-types.xml 882 + + + Local functions + + You'll have noticed that Haskell's syntax for defining a + variable looks very similar to its syntax for defining a + function. This symmetry is preserved in let and + where blocks; we can define local + functions just as easily as local + variables. All of the same syntax + applies as at the top level: we can use multiple equations, + patterns, and guards. + + &LocalFunction.hs:pluralise; + + In this example, we define and use a local function + plural using several equations. Local + functions can freely use variables from the scopes that + enclose them; here, we use word from the + definition of the outer function + pluralise. In the definition of + pluralise, the map + function (which we'll be revisiting in the next chapter) + applies the local function plural to + every element of the counts list. + addfile ./examples/ch03/LocalFunction.hs hunk ./examples/ch03/LocalFunction.hs 1 +{-- snippet pluralise --} +pluralise :: String -> [Int] -> [String] +pluralise word counts = map plural counts + where plural 1 = "1 " ++ word + plural n = show n ++ " " ++ word ++ "s" +{-- /snippet pluralise --} }