1
votes

I Started writing Haskell code . I tried to write a fibonacci function using Guards -

    fibo :: (Num z, Ord z) => z -> z
    fibo d
    | d <= 0 = 0
    | d == 1 = 1
    | otherwise = fibo (d-1) + fibo (d-2)

I got this error :-

Illegal type signature: ‘(Num z, Ord z) => z -> z fibo d’ Perhaps you intended to use ScopedTypeVariables In a pattern type-signature

However another function - replicate i have written in a similar way which compiled and worked fine . I can write fibonacci in another way , but I want to know what the error was

1
Can you include the context above the fibo definition? Why is it indented?Cirdec
In GHC before 7.10, use -fwarn-tabs. In more recent GHC, pay attention to this warning, which is now enabled by default. And don't use tabs.dfeuer
BTW, you should probably enable ScopedTypeVariables anyway, and learn to use it. It's a very helpful and friendly extension.dfeuer

1 Answers

9
votes

The indentation in your program is wrong and StackOverflow's weird treatment of tabs made the indentation in your question wrong in a different way.

  1. Your program should be indented like this:

    fibo :: (Num z, Ord z) => z -> z
    fibo d
      | d <= 0 = 0
      | ...
    

    The first two lines should start in the same column and the lines with guards should be more indented than those lines.

  2. The program as displayed in your question is wrong in a different way than the error you mentioned: the lines with guards must be more indented than the preceding lines. This happened because StackOverflow has a nonstandard treatment of tab characters. Don't use tabs.

  3. Your error is consistent with GHC viewing your program as indented like this:

    fibo :: (Num z, Ord z) => z -> z
      fibo d               -- wrong, must start in same column as previous line
      | d <= 0 = 0
      | ...
    

    We can reconstruct that your original program must have been

    <sp><sp><sp><sp>fibo :: (Num z, Ord z) => z -> z
    <tab>           fibo d
    <tab>           | d <= 0 = 0
    <tab>           | ...
    

    Don't use tabs.