Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I curry one of the patterns but not the other in my pattern matching?

I have I have a function with two arguments that I have to pattern match over. If I use currying on the first pattern it won't compile:

  drop' :: Int -> [a] -> [a] 
  drop' 0  = id -- ghci: "Equations for drop' have different numbers of arguments"
  drop' n (x:xs) = drop' (n-1) xs

The compiler gives this output:

99.hs:106:3:
Equations for drop' have different numbers of arguments
  99.hs:106:3-15
  99.hs:107:3-33
In an equation for `split':
    split xs n
      = (take' n xs, drop' n xs)
      where
          take' 0 _ = []
          take' n (x : xs) = x : take (n - 1) xs
          drop' 0 = id
          drop' n (x : xs) = drop' (n - 1) xs  
 Failed, modules loaded: none.

If I only give the curried pattern, however, then it compiles:

  drop' :: Int -> [a] -> [a] 
  drop' 0  = id -- compiles

What gives?

like image 320
Guildenstern Avatar asked Mar 23 '13 23:03

Guildenstern


3 Answers

The only explanation I could find (http://www.haskell.org/pipermail/haskell-cafe/2009-March/058456.html):

The problem is mostly syntactical, in the sense that most occurrences of definitions with a different number of arguments are plain typos. The other might be implementation issues: it makes pattern match rules more complex.

like image 192
Koterpillar Avatar answered Oct 31 '22 09:10

Koterpillar


I can't tell you why for sure, but this is a known limitation. All cases of the same function have to have the same number of arguments.

like image 21
Peter Hall Avatar answered Oct 31 '22 10:10

Peter Hall


This is an annoying "feature" of GHC, to be sure, but to fix it, you can do this:

drop' n = \(x:xs) -> drop' (n-1) xs

You have to curry both or neither, and both to the same number of arguments. If this is a lint check, that's great: but I wish there were a compiler option to turn it on/off.

like image 1
jpaugh Avatar answered Oct 31 '22 09:10

jpaugh