English 中文(简体)
Haskell 只对结果类型应用函数箭头吗?
原标题:Haskell apply function arrow to only result type?

在 Haskell 中能否将函数箭头类型构建器 (- & gt;) 应用到右侧类型( 例如:(- gt; a), 以获得类型构建器 * - & gt; *?

最佳回答

不,这是目前不可能的。Haskell 类型系统有某些限制,使得它能对大多数案件有用和方便,这是其中的一个限制。你最好的选择是使用新类型。

newtype FuncFlip r a = FuncFlip { unFuncFlip :: a -> r }

新建类型只是用来帮助编译者正确键入和进行类型方向调度( 类型类) 的标记。 假设您想要翻转类型参数以提供某个类型类实例。 这仅仅意味着每当您想要使用该类型类函数时, 您必须用 < code> FuncFlip 来设置任何特定输入, 并且用 < code> unFuncFlip 来解析任何特定输出。 这比想要的略多一点动词, 但实际上并不坏, 因为它迫使您明确指定您想要使用的类型类的哪个实例 。

您可以为此创建 < a href=>"的示例, http://hackage.haskell.org/packages/archive/ newtype/ 0.2/doc/html/ control-Newtype.html" rel=“ nofollow noreferrer>Newty , 这可能对你来说不方便。

instance Newtype (FuncFlip r a) (a -> r) where
  pack = FuncFlip
  unpack = unFuncFlip

更多信息:

问题回答

暂无回答




相关问题
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?