2

I'm a Haskell newbie, so please excuse me if you find this question trivial:

How would I get GHCi to accept a declaration of this sort: let foo = fmap (*3) . fmap (+10)?

I tried adding a type declaration to foo (let foo :: [Int] -> [Int] = etc) to make the functor type explicit but the compiler responds Illegal Signature.

Thanks!

EDIT - Apparently there are quite a few ways to do this. I picked Tikhon's answer because his was among the first, and also fairly intuitive. Thanks, everyone!

planarian
  • 2,047
  • 18
  • 18

4 Answers4

9

To give type signatures in ghci, the best way, not requiring any extensions, is to separate the signature and the binding with a semicolon,

let foo :: Num n => [n] -> [n]; foo = map (*3) . map (+ 10)
Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
5

You can give the expression (that is, fmap (* 3) . fmap (+ 10)) a signature rather than giving it to foo. So:

let foo = fmap (* 3) . fmap (+ 10) :: [Int] -> [Int]
Tikhon Jelvis
  • 67,485
  • 18
  • 177
  • 214
3

The full error reads

Illegal signature in pattern: [Int] -> [Int]
    Use -XScopedTypeVariables to permit it

The solution is to run

:set -XScopedTypeVariables

Now you can try running your let foo :: [Int] -> [Int] = fmap (*3) . fmap (+10) and it will work.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
1
:set -XNoMonomorphismRestriction
Gabriella Gonzalez
  • 34,863
  • 3
  • 77
  • 135