chain up elements with an associative binary operation

I am an intermediate schemer, but only a haskell beginner. Here is my problem: Suppose you have an associative binary operation, says (>>=) . Is there a polyvariadic function p such that p (>>=) hgfe = h >>= g >>= f >>= e ? I am asking this question because this question says it is possible if the binary operation takes inputs of the same type. I wonder if thi

用关联二元运算链接元素

我是一名中间策划者,但只是一名haskell初学者。 这是我的问题: 假设你有一个关联的二元操作,说(>>=) 。 是否存在一个多变量函数p ,使得p (>>=) hgfe = h >>= g >>= f >>= e ? 我在问这个问题,因为这个问题说如果二进制操作接受相同类型的输入是可能的。 我想知道这是否可以推广。 编辑1:我尝试修改http://okmij.org/ftp/Haskell/vararg-fn.lhs(可变数量的可变类型参数部分)中的

Partial application in haskell with multiple arguments

Given some function f(x1,x2,x3,..,xN) it is often useful to apply it partially in several places. For example, for N=3 we could define g(x)=f(1,x,3). However, the standard partial application in Haskell does not work this way and only allows us to partially apply a function by fixing its first arguments (because all functions actually only take one argument). Is there any simple way to do some

在具有多个参数的haskell中部分应用

给定函数f(x1,x2,x3,...,xN)通常可以将它部分应用于多个地方。 例如,对于N = 3,我们可以定义g(x)= f(1,x,3)。 然而,Haskell中的标准部分应用程序不能以这种方式工作,只允许我们通过修复其第一个参数来部分应用函数(因为所有函数实际上只带一个参数)。 有没有简单的方法来做这样的事情: g = f _ 2 _ g 1 3 输出值为f 1 2 3 ? 当然,我们可以做一个lambda函数 g=(x1 x3 -> f x1 2 x3) 但我觉得这很

How do I get the type signature of the range function in Haskell?

Many functions in Haskell made up of special characters in Haskell are infix functions. These include * , + , == , / , etc. To get the type signatures of such functions you put the function in parentheses and execute :t , like so: GHCi> :t (==) (==) :: Eq a => a -> a -> Bool I wanted to try and get the type signature of the range function, [a..a] , but it seems that this function i

我如何获得Haskell中范围函数的类型签名?

Haskell中由Haskell中的特殊字符组成的许多函数都是中缀函数。 这些包括* , + , == , /等。要获得这些函数的类型签名,请将函数放在括号中并执行:t ,如下所示: GHCi> :t (==) (==) :: Eq a => a -> a -> Bool 我想尝试获取范围函数[a..a]的类型签名,但似乎该函数是中缀,但只能在列表[] 。 我尝试了以下所有内容,但都没有成功: GHCi> :t (..) <interactive>:1:2: parse error on input `..' GH

Why is function definition for all types at once not allowed in Haskell?

It's a feature, and actually is very fundamental. It boils down to a property known as parametricity in programming language theory. Roughly, that means that evaluation should never depend on types that are variables at compile time. You cannot look at a value where you do not know its concrete type statically. Why is that good? It gives much stronger invariants about programs. For exa

为什么Haskell不允许所有类型的函数定义?

这是一个功能,而且实际上是非常重要的。 它归结为在编程语言理论中被称为参数性的属性。 粗略地说,这意味着评估不应该依赖编译时变量的类型。 在静态不知道具体类型的情况下,您无法查看值。 为什么这很好? 它给了程序更强大的不变性。 例如,你只从类型知道a -> a必须是身份函数(或分歧)。 类似的“自由定理”适用于许多其他多态函数。 参数化也是更高级的基于类型的抽象技术的基础。 例如,类型ST sa在Haskell

Haskell: What is Weak Head Normal Form?

What does Weak Head Normal Form (WHNF) mean? What does Head Normal form (HNF) and Normal Form (NF) mean? Real World Haskell states: The familiar seq function evaluates an expression to what we call head normal form (abbreviated HNF). It stops once it reaches the outermost constructor (the “head”). This is distinct from normal form (NF), in which an expression is completely evaluated. You

哈斯克尔:什么是弱头正常形式?

弱头标准形式 (WHNF)是什么意思? 头部正常形式 (HNF)和正常形式 (NF)是什么意思? 真实世界Haskell说: 熟悉的seq函数将表达式评估为我们称之为头标准形式(简称HNF)的表达式。 它一旦到达最外层的构造函数(“头部”)就会停止。 这不同于正常形式(NF),其中表达被完全评估。 您还将听到Haskell程序员提到弱头标准格式(WHNF)。 对于正常数据,弱磁头标准形式与标准标准形式相同。 功能上的区别只在于,而

How does deriving work in Haskell?

Algebraic Data Types (ADTs) in Haskell can automatically become instances of some typeclasses (like Show , Eq ) by deriving from them. data Maybe a = Nothing | Just a deriving (Eq, Ord) My question is, how does this deriving work, ie how does Haskell know how to implement the functions of the derived typeclass for the deriving ADT? Also, why is deriving restricted to certain typeclasses

在Haskell中如何派生工作?

Haskell中的代数数据类型(ADT)可以通过派生它们自动变成某些类型类的实例(如Show , Eq )。 data Maybe a = Nothing | Just a deriving (Eq, Ord) 我的问题是,这是如何deriving工作的,即Haskell如何知道如何实现派生类型类派生ADT的函数? 此外,为什么只能deriving到某些类型类? 为什么我不能编写我自己可以派生的类型类? 简短的回答是,魔术:-)。 这就是说,自动派生被烘焙到Haskell规范中,并且每个编译

Observing lazyness in Haskell

Is it possible to write a Haskell function which depends on whether values are calculated already or are thunks? Eg if lazyShow :: [Int] -> String shows thunks as ? and calculated values normally, in GHCi we would see > let nats = [0..] > lazyShow nats 0 : ? > nats !! 5 5 > lazyShow nats 0 : 1 : 2 : 3 : 4 : ? Clearly, lazyShow cannot have the type you state. If the str

观察哈斯克尔的懒惰

是否有可能编写一个Haskell函数,取决于值是已经计算好还是为thunk? 例如,如果lazyShow :: [Int] -> String显示thunk为? 并通常计算值,在GHCi中我们会看到 > let nats = [0..] > lazyShow nats 0 : ? > nats !! 5 5 > lazyShow nats 0 : 1 : 2 : 3 : 4 : ? 显然, lazyShow不能拥有你lazyShow的类型。 如果字符串应该取决于当前的评估状态,那么IO String就是您所期望的最好的结果。 如果你感

What's wrong with using Identity monad with mmultP when using repa?

I do not understand why this program using repa: import Data.Array.Repa import Data.Array.Repa.Algorithms.Matrix import Data.Functor.Identity go = runIdentity $ do let mat = fromListUnboxed (ix2 2 2) [1..4] let ins = fromListUnboxed (ix2 2 1) [1, 1] mmultP mat ins is giving me the following warning: Data.Array.Repa: Performing nested parallel computation sequentially. You've probably

在使用repa时使用Identity monad和mmultP有什么问题?

我不明白为什么这个程序使用了repa: import Data.Array.Repa import Data.Array.Repa.Algorithms.Matrix import Data.Functor.Identity go = runIdentity $ do let mat = fromListUnboxed (ix2 2 2) [1..4] let ins = fromListUnboxed (ix2 2 1) [1, 1] mmultP mat ins 给我以下警告: Data.Array.Repa: Performing nested parallel computation sequentially. You've probably called the 'compute' or 'copy' func

Logical AND strictness with IO monad

I am trying to write a simple program in Haskell. It should basically run two shell commands in parallel. Here is the code: import System.Cmd import System.Exit import Control.Monad exitCodeToBool ExitSuccess = True exitCodeToBool (ExitFailure _) = False run :: String -> IO Bool run = (fmap exitCodeToBool) . system main = liftM2 (&&) (run "foo") (run "bar") But command "foo

IO monad的逻辑和严格性

我正在尝试在Haskell中编写一个简单的程序。 它应该基本上并行运行两个shell命令。 代码如下: import System.Cmd import System.Exit import Control.Monad exitCodeToBool ExitSuccess = True exitCodeToBool (ExitFailure _) = False run :: String -> IO Bool run = (fmap exitCodeToBool) . system main = liftM2 (&&) (run "foo") (run "bar") 但命令“foo”返回ExitFailure,我希望“bar”永远不会运行。

Wadler, "Monads for Functional Programming," Section 2.8

Edit II: Ah, okay: I wasn't understanding how a and b were being bound in the definition of eval! Now I do. If anyone's interested, this is a diagram tracking a and b. I'm a pretty big fan of diagrams. Drawing arrows really improved my Haskell, I swear. A Diagram of an eval call (PDF) Sometimes I feel really dense. In section 2.8 of Wadler's "Monads for Functional P

Wadler,“用于函数式编程的Monads”,2.8节

编辑二:嗯,好吧:我不理解a和b如何被绑定在eval的定义中! 现在我知道了。 如果任何人有兴趣,这是一个跟踪a和b的图。 我是图表的忠实粉丝。 绘制箭头确实改善了我的Haskell,我发誓。 评估电话图(PDF) 有时候我觉得很密集。 在Wadler的“函数式编程Monads”第2.8节中,他将状态引入一个简单的评估函数。 原始(非一元)函数使用一系列let表达式跟踪状态,并且很容易遵循: data Term = Con Int | Div Term Term d