English 中文(简体)
Is it possible to use unary function instead of binary in `flip`?
原标题:
  • 时间:2009-11-11 14:09:04
  •  标签:
  • haskell
  • flip

The type of the Prelude function flip is:

flip :: (a -> b -> c) -> b -> a -> c

I.e., it takes one binary function and two arguments.

The type of the Prelude function id is:

id :: a -> a

But the type of flip id is:

flip id :: a -> (a -> b) -> b

How is it possible to apply flip to id when id is a unary function and flip requires binary function for the first arg?

btw. flip id is similar to x f -> f x

最佳回答

Haskell makes id fit the type of the first argument to flip by setting a = b -> c. So:

flip :: ( a       -> b -> c) -> b ->  a       -> c
flip :: ((b -> c) -> b -> c) -> b -> (b -> c) -> c
flip id ::                      b -> (b -> c) -> c

where id is taken to be of type

id :: (b -> c) ->  b -> c

which is equivalent to

id :: (b -> c) -> (b -> c)

i.e. a specialisation of id that only applies to unary functions.

Edit: I think I might rephrase my first line as:
Haskell deduces that id fits the type of the first argument to flip if a = b -> c.
In case that s any clearer.

问题回答

Nefrubyr explains it very well.
Another way to (hopefully) make this a bit more intuitive is to think of the function application operator ($).

($) is a specialized form of id:

($) :: (a -> b) -> (a -> b)
($) = id

I ve seen the definition (#) = flip ($), such that you can write the argument before the function its applied to: obj # show.

Obviously, since ($) is just a specialized form of id, you could also write: (#) = flip id





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签