I need some clarification about laziness with Haskell. If I have this function: myFunction arg | arg == 1 = a | arg == 2 = a*b | arg == 3 = b+c | otherwise = (a+b)*c where a = ... b = ... c = ... d = ... When I call myFunction 1 , Haskell will evaluate only the a = ... function, neither b nor c , nor d . But if I write myFunction
我需要一些关于Haskell的懒惰的澄清。 如果我有这个功能: myFunction arg | arg == 1 = a | arg == 2 = a*b | arg == 3 = b+c | otherwise = (a+b)*c where a = ... b = ... c = ... d = ... 当我调用myFunction 1 ,Haskell将只评估a = ...函数,既不b也不c ,也不d 。 但是,如果我写 myFunction arg | arg == 1 = a | arg == 2 = a*b | arg == 3
Consider the Object-Oriented Languages: Most people coming from an object-oriented programming background, are familiar with the common and intuitive interfaces in various languages that capture the essence of Java's Collection & List interfaces. Collection refers to a collection of objects which doesn't necessarily have an natural ordering/indexing. A List is a collection which h
考虑面向对象的语言: 大多数来自面向对象编程背景的人都熟悉各种语言中常见且直观的界面,这些界面捕捉了Java的Collection & List界面的本质。 Collection是指不一定具有自然顺序/索引的对象集合。 List是一个具有自然顺序/索引的集合。 这些接口抽象了Java中的许多库数据结构,就像其他语言中的等价接口一样,并且需要对这些接口有深入的了解才能与大多数库数据结构有效地协作。 过渡到Haskell: Haskell有一个类型
Could anyone give some pointers on why the impure computations in Haskell are modelled as monads? I mean monad is just an interface with 4 operations, so what was the reasoning to modelling side-effects in it? Suppose a function has side effects. If we take all the effects it produces as the input and output parameters, then the function is pure to the outside world. So for an impure funct
任何人都可以提供一些关于为什么Haskell中的不纯计算被建模为monads的指示吗? 我的意思是monad仅仅是一个包含4个操作的接口,那么为它建模的副作用是什么原因呢? 假设一个函数有副作用。 如果我们将所产生的所有效果作为输入和输出参数,那么该函数对外界来说是纯粹的。 所以对于不纯的功能 f' :: Int -> Int 我们将RealWorld添加到考虑中 f :: Int -> RealWorld -> (Int, RealWorld) -- input some states
In category theory, a monad is the composition of two adjoint functors. For example, the Maybe monad is the free pointed-set functor composed with the forgetful functor. Likewise, the List monad is the free monoid functor composed with the forgetful functor. Monoid is one of the simplest algebraic structures, so I wonder if programming can benefit from more complex ones. I didn't find th
在分类理论中,monad是两个伴随函数的组合。 例如,Maybe monad是由健忘函数组成的免费指向函子。 同样,列表monad是由健忘的函子组成的自由monoid函子。 Monoid是最简单的代数结构之一,所以我想知道编程能否从更复杂的代码中受益。 我没有在标准的Haskell软件包中找到免费的组monad,所以我在这里定义它 data FreeGroup a = Nil | PosCons a (FreeGroup a) | NegCons a (FreeGroup a) ==运算符定义为NegCons x (PosCons
Every time someone promises to "explain monads", my interest is piqued, only to be replaced by frustration when the alleged "explanation" is a long list of examples terminated by some off-hand remark that the "mathematical theory" behind the "esoteric ideas" is "too complicated to explain at this point". Now I'm asking for the opposite. I h
每当有人承诺“解释单子”时,我的兴趣就会激化,只有当所谓的“解释”是一个长长的清单时,才会被挫折所取代,这些清单是由一些副手所断言的“隐秘背后的数学理论”想法“在这一点上”太复杂了,无法解释“。 现在我要求相反。 我对类别理论有着坚实的把握,我不害怕图追逐,Yoneda的引理或派生的函数(事实上在单类和单数意义上的monad和adjunctions上)。 有人能给我一个关于函数式编程中monad的清晰简洁的定义吗? 例子越少越好
From a gentle introduction to Haskell, there are the following monad laws. Can anyone intuitively explain what they mean? return a >>= k = k a m >>= return = m xs >>= return . f = fmap f xs m >>= (x -> k x >>= h) = (m >>= k) >>= h Here is my attempted explanation: We expect the return function to wrap a so tha
从Haskell的一个温和的介绍,有以下monad法律。 任何人都可以直观地解释他们的意思吗? return a >>= k = k a m >>= return = m xs >>= return . f = fmap f xs m >>= (x -> k x >>= h) = (m >>= k) >>= h 这是我的尝试解释: 我们期望返回函数包装a以便它的monadic本质是微不足道的。 当我们将它绑定到函数时,没有单调效果,它应该
I'm having some trouble figuring out /how/ the bind operator would actually bind together the following State monads: pop :: State [Int] Int pop = do (x:xs) <- get put xs return x push :: Int -> State [Int] () push x = do xs <- get put (x:xs) doStuff :: State [Int] () doStuff = do pop x <- pop
我在计算/绑定操作符如何将下列状态单元绑定在一起时遇到了一些麻烦: pop :: State [Int] Int pop = do (x:xs) <- get put xs return x push :: Int -> State [Int] () push x = do xs <- get put (x:xs) doStuff :: State [Int] () doStuff = do pop x <- pop push 5 push x 采取doStuff ,可以解决以下问
I have some functions written in C that I call from Haskell. These functions return IO (CInt) . Sometimes I want to run all of the functions regardless of what any of them return, and this is easy. For sake of example code, this is the general idea of what's happening currently: Prelude> let f x = print x >> return x Prelude> mapM_ f [0..5] 0 1 2 3 4 5 Prelude> I get my de
我有一些用C编写的函数,我从Haskell调用。 这些函数返回IO (CInt) 。 有时我想运行所有的功能,而不管它们中的任何一个返回,这很容易。 出于示例代码的缘故,这是目前发生的一般想法: Prelude> let f x = print x >> return x Prelude> mapM_ f [0..5] 0 1 2 3 4 5 Prelude> 我得到了我想要的副作用,我不关心结果。 但是现在我需要在第一个没有返回我想要的结果的项目之后立即停止执行。 假设返回值4
I'm trying to compile some code in Real World Haskell - Chapter 24. LineCount.hs. I have not made any changes to the code. However, when I do: ghc -O2 --make -threaded LineCount.hs (as instructed in the book), I get the message: MapReduce.hs:6:7: Not in scope: `rnf' What might I be doing wrong? A quick search showed up that there was some trouble with the packages parallel and stric
我正在尝试编译Real World Haskell中的一些代码 - 第24章LineCount.hs。 我没有对代码做任何修改。 但是,当我这样做时: ghc -O2 --make -threaded LineCount.hs (按照书中的说明),我得到这样的信息: MapReduce.hs:6:7: Not in scope: `rnf' 我可能会做错什么? 通过快速搜索发现,过去的软件包并行和严格并发性存在一些问题,重新安装它们可以解决问题。 但是,我尝试过,并没有奏效。 此外,有人指出,该问题
Yes, these ones: {-#LANGUAGE TypeOperators, RankNTypes #-} import Control.Morphism.Zygo import Control.Morphism.Prepro import Control.Morphism.Histo import Control.Functor.Algebra import Control.Functor.Extras import Control.Functor.Fix import Control.Comonad.Cofree zygohistomorphic_prepromorphism :: Functor f => Algebra f b -> GAlgebra f (ZygoT (Cofree f) b) a -> (f :~> f
是的,这些: {-#LANGUAGE TypeOperators, RankNTypes #-} import Control.Morphism.Zygo import Control.Morphism.Prepro import Control.Morphism.Histo import Control.Functor.Algebra import Control.Functor.Extras import Control.Functor.Fix import Control.Comonad.Cofree zygohistomorphic_prepromorphism :: Functor f => Algebra f b -> GAlgebra f (ZygoT (Cofree f) b) a -> (f :~> f)