3

A definition like

mergeLists :: Ord a => [a] -> [a] -> [a]
mergeLists [] = id
mergeLists l [] = l
mergeLists (x:xs) (y:ys) = if y < x
    then y : mergeLists (x:xs) ys
    else x : mergeLists xs (y:ys)

leads to the error

MergeLists.hs:3:1: error:
    Equations for ‘mergeLists’ have different numbers of arguments
      MergeLists.hs:3:1-18
      MergeLists.hs:4:1-19
  |
3 | mergeLists [] = id
  | ^^^^^^^^^^^^^^^^^^...

With the line in question rewritten as mergeLists [] l = l the error is gone.

Does Haskell prohibit the mixture of point-free and non-point-free clauses in a single function definition?

fghzxm
  • 1,185
  • 7
  • 19
  • From the two suggested questions, the "Defining a function [...]" one focuses on "why", while the "Number of arguments [...]" one focuses on "how". – duplode May 21 '18 at 12:13

1 Answers1

4

When you define a function in Haskell through this method, each declaration must have the same number of arguments. This are for reasons involving possible typos, and easier pattern-matching.

Since your first line only includes one argument, [], it causes an error since all other definitions include two arguments.

This is a prohibition of mixing pointed and point-free styles in this particular case, yes, but it seems to be to avoid typos as mentioned.

AJF
  • 11,767
  • 2
  • 37
  • 64