Why can't Haskell automatically solve the number of arguments?

I'm new to Haskell, and the following behavior confused me:

I have a function called dealWithIt. It looks like this:

dealWithIt :: (Show a) => [a] -> String
dealWithIt = foldl f ""
  where f memo x = memo ++ (show x)

Everything is fine, it works as expected, it gets a list of shortcuts and combines them into one line.

As far as I understand, it doesn’t matter if I explicitly indicate the argument received, if it can be passed to the base chain of functions. This means that the following two definitions should be equivalent:

dealWithIt xs = foldl f "" xs
dealWithIt = foldl f ""

So far so good. Suppose I want to add a special script from the template now:

dealWithIt [] = "Empty list :("

Everything is strange here. If I do not specify an explicit xs argument, I get the following error:

Equations for ‘dealWithIt’ have different numbers of arguments

, , Haskell , , ?

+4
1

. :

f p0 p1 = e0
f p2 p3 = e1

.

; Haskell case:

f x0 x1 = case (x0, x1) of
    (p0, p1) -> e0
    (p2, p3) -> e1

, ,

f p0 p1 = e0
f p2 = e1 -- `e1` is a function

f x0 x1 = case (x0, x1) of
    (p0, p1) -> e0
    (p2, _) -> e1 x1 -- Note that the argument to `e1` has to be supplied explicitly

-, .

, foldr:

foldr f z [] = z
foldr f z (x:xn) = f x (foldr f z xn)

, f :

foldr z [] = z
foldr f z (x:xn) = f x (foldr f z xn)

: , . , , , . (, , z , f , z , f x (foldr f z xn) , foldr . .)

+4

Source: https://habr.com/ru/post/1616262/


All Articles