fac n = if n < 2 then 1 else n * fac (n-1)
main = do
putStrLn "Enter a number: "
number <- getLine
print $ number >>= fac
I do not know how to write a recursive factorial function without if statements. Our professor said somethings about lambda calculus.
Pattern matching and guards are two particularly straightforward ways. Guards are essentially another syntax for if-then-else; they look like this:
Unlike if-then-else, they cleanly support multiple conditions; one could also write, for example,
which would look much less pretty in if-then-else form.
With pattern matching, one would typically write multiple defining equations:
For numbers in particular, this also desugars to essentially an if-then-else; but for data types with less compiler integration often cannot be emulated with if-then-else, and also often leads to very natural-looking code.
Another very nice approach would be to push your recursion into existing Prelude functions; the more you can spot iteration patterns in practice the more bugs you can avoid by not re-implementing the same loops over and over. For this one, you might use
product
and the special enumeration syntax:A more advanced (and significantly worse) technique would be to define a new kind of number; e.g. Church numerals allow the producer of the number to drive the recursion, and the consumer (here,
fac
) to just supply base cases. In this style, you might see something like this:(But note well that this requires a very special kind of number -- certainly the type of
fac
is not one of a function that could acceptInt
orInteger
!) This joke is taken to its logical and horrifying conclusion in The Evolution of a Haskell Programmer.