Different numbers of arguments when matching patterns.

I ran into a problem that I really don't understand. I thought I could write such code in Haskell:

foo :: Maybe Int -> Int
foo Nothing = 0
foo Just x = x

But when I try to compile it, I get an error:

The equations for 'foo have different numbers of arguments

I can fix this by changing my code to the following:

foo :: Maybe Int -> Int
foo Nothing = 0
foo (Just x) = x

Which makes me think that the GHC interprets Justas an argument foo. But Haskell forbids using capital letters to run variable names, so I don't think there should be any ambiguity here. What's happening?

+4
source share
2 answers

, , Just, ! Haskell , , foo Just x = x - . :

Prelude> let foo Just x = x

<interactive>:2:9:
    Constructor β€˜Just’ should have 1 argument, but has been given none
    In the pattern: Just
    In an equation for β€˜foo’: foo Just x = x

, :

Prelude> data Justice = Just
Prelude> let foo Just x = x
Prelude> :t foo
foo :: Justice -> t -> t
Prelude> foo Just ()
()

Just ( ), - -, Just x , " " " . (, , Nothing, .)

+11

, . foo, foo Just x, - ( foo (Int -> Maybe Int) -> Int -> Int, ). , , .

. ?

foo Just x : xs = ...
+4

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


All Articles