Understanding the Type of this Haskell Function

I am new to Haskell and I have a question about the following code:

a x (b:bs) = a (b x) bs
a x b = x []

What is the common type of these patterns?

With :infoa we get:([t1] -> t) -> [([t1] -> t) -> [t1] -> t] -> t

However, I can’t understand why. Does it have aonly two "inputs" or not?

+4
source share
2 answers

Do I have only two "inputs" or not?

Yes, you have two options (technically one due to how currying works).

C: info a I get: ([t1] -> t) -> [([t1] -> t) -> [t1] -> t] -> t

, [t1] -> t ( , t1 t), [([t1] -> t) -> [t1] -> t] ( , [t1] -> t t1 t), t.

, .

:

a x (b:bs) =

- , - . - , .

, , , :

x :: ?1
b :: ?2
bs :: [?2]
a :: ?1 -> [?2] -> ?3

:

a (b x) bs

b x, b , x. b x a, b x. , ?2 ?1 -> ?1 :

x :: ?1
b :: ?1 -> ?1
bs :: [?1 -> ?1]
a :: ?1 -> [?1 -> ?1] -> ?3

(b bs , b, , ):

x []

x . , , - . x [] a, x, ?3. , ?1 = [?4] -> ?3 :

a :: ([?4] -> ?3) -> [([?4] -> ?3) -> ([?4] -> ?3)] -> ?3

-> -, :

a :: ([?4] -> ?3) -> [([?4] -> ?3) -> [?4] -> ?3] -> ?3

, , , , . ?3 = t ?4 = t1, info (, , - , ).

+6

, :

a :: t -> [t -> t] -> t1
a x (b:bs) = a (b x) bs

. , , , .

:

a :: t -> [t -> t] -> t
a x [] = x

, . , . - , - :

a :: t -> [t -> t] -> t
a = foldl (flip ($))

, .

a x [] = x []

: x - , somethings -. , , t1 t2. , x type is t, t [t1] -> t2:

a :: ([t1] -> t2) -> [([t1] -> t2) -> ([t1] -> t2)] -> t2

! , :

a x ls = foldl (flip ($)) x ls $ []

.

+1

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


All Articles