1
factorial :: Integer -> Integer
factorial n = product [1..n]

The following is fine:

let factorial n = product [1..n]

I do not see how to add type declarations in interactive shell.

Jacob
  • 349
  • 1
  • 2
  • 12
  • Why do you want to add type declarations? Haskell will always attach the most generic signature to the function. In this case: `(Enum a, Num a) => a -> a` – Willem Van Onsem Sep 15 '15 at 14:05
  • For testing purposes. – Jacob Sep 15 '15 at 14:13
  • 2
    possible duplicate of [How to provide explicit type declarations for functions when using GHCi?](http://stackoverflow.com/questions/3093133/how-to-provide-explicit-type-declarations-for-functions-when-using-ghci) – Sam van Herwaarden Sep 15 '15 at 14:23

2 Answers2

5

If you want to specify the type signature yourself you can do this in ghci using semicolons, i.e.:

let factorial :: Integer -> Integer; factorial n = product [1..n]
Sam van Herwaarden
  • 2,321
  • 14
  • 27
  • 2
    To be super picky, the reason this works is that it is one statement, a let statement containing two declarations. It parses as `let { factorial :: Integer -> Integer; factorial n = product [1..n] }`. – Reid Barton Sep 15 '15 at 14:58
5

Next to the multi-line settings explained here, you can use this instead if you don't want to write semicolons.

λ> :{
λ> | let factorial :: Integer -> Integer
λ> |     factorial n = product [1..n]
λ> :}
λ> :t factorial
factorial :: Integer -> Integer
Community
  • 1
  • 1
Cirquit
  • 464
  • 2
  • 9