monads

How to Better Iterate over State in Clojure (monad?)

情到浓时终转凉″ 提交于 2019-12-07 01:12:09
问题 I just wrote this code: (defn parameters [transform-factory state] (lazy-seq (let [[r1 state] (uniform state) [r2 state] (uniform state) [t state] (transform-factory state)] (cons [t [r1 r2]] (parameters transform-factory state))))) (defn repeated-transform [mosaic n transform-factory state] (reduce transform-square mosaic (take n (parameters transform-factory state)))) the parameters function generates a lazy sequence of values generated from the state , which are used to parameterise a

Why runState signature has only state argument?

痴心易碎 提交于 2019-12-07 00:58:41
问题 A real-life example: If I'm in a good mood('good state'), when manager asks me about estimates, I give him a solid answer, but dares he to do that 3 times in a row , without some free snacks in between, my mood changes(I get to 'bad state') and the next 3 times he approaches I ask him not to disturb me with any of his nonsense. Here's a log of my usual day: [ Mood: Good, Patience: 3 ] -- 11:00 am, I'm happy ESTIMATE -> "bla bla 6", [ Mood: Good, Patience: 2 ] ESTIMATE -> "bla bla 1", [ Mood:

Is print in Haskell a pure function?

让人想犯罪 __ 提交于 2019-12-07 00:31:43
问题 Is print in Haskell a pure function; why or why not? I'm thinking it's not, because it does not always return the same value as pure functions should. 回答1: A value of type IO Int is not really an Int . It's more like a piece of paper which reads "hey Haskell runtime, please produce an Int value in such and such way". The piece of paper is inert and remains the same, even if the Int s eventually produced by the runtime are different. You send the piece of paper to the runtime by assigning it

Does a Powerset-over-Reader monad exist?

半世苍凉 提交于 2019-12-07 00:24:58
问题 The canonical 'Monad instance' for environment sharing plus nondeterminism is as follows (using pseudo-Haskell, since Haskell's Data.Set isn't, of course, monadic): eta :: a -> r -> {a} -- '{a}' means the type of a set of a's eta x = \r -> {x} bind :: (r -> {a}) -> (a -> r -> {b}) -> r -> {b} m `bind` f = \r -> {v | x ∈ m r, v ∈ f x r} Generally, when trying to combine a 'container' monad like Powerset (List, Writer, etc) with a second monad m (here, roughly, Reader), one 'wraps' m around the

What effects are modeled by the stream (infinite list) monad?

Deadly 提交于 2019-12-06 23:52:09
问题 Various instances of monads model different type of effects: for example, Maybe models partiality, List non-determinism, Reader read-only state. I would like to know if there is such an intuitive explanation for the monad instance of the stream data type (or infinite list or co-list), data Stream a = Cons a (Stream a) (see below its monad instance definition). I've stumbled upon the stream monad on a few different occasions and I would like to understand better its uses. data Stream a = Cons

What can we do with Alternative but cannot do with Monoid?

爱⌒轻易说出口 提交于 2019-12-06 23:26:45
问题 I read Why MonadPlus and not Monad + Monoid? and I understand a theoretical difference, but I cannot figure out a practical difference, because for List it looks the same. mappend [1] [2] == [1] <|> [2] Yes. Maybe has different implementations mappend (Just "a") (Just "b") /= (Just "a") <|> (Just "b") But we can implement Maybe Monoid in the same way as Alternative instance Monoid (Maybe a) where Nothing `mappend` m = m m `mappend` _ = m So, can someone show the code example which explains a

Does Writer Monad guarantee right associative concatenation?

[亡魂溺海] 提交于 2019-12-06 20:00:00
问题 It was claimed in Validations in Haskell that use of a Writer guarantees right-associative concatenation. However, this example seems to show otherwise. What's the correct answer? {-# LANGUAGE OverloadedStrings #-} import Control.Monad.Writer import Data.String data TM = TMempty | TMappend TM TM | TMfromString String instance IsString TM where fromString = TMfromString instance Monoid TM where mempty = TMempty mappend = TMappend instance Show TM where showsPrec d TMempty = showString "\"\""

I can't understand Wikipedia's definition of “applicative functor”

旧时模样 提交于 2019-12-06 19:05:09
问题 Studying functors, applicative functors and monads in Haskell, I found this definition on Wikipedia: In functional programming, specifically Haskell, an applicative functor is a structure that is like a monad ( return , fmap , join ) without join , or like a functor with return . I can't understand: it seems to me that providing return (i.e. pure ) to a functor is not sufficient to obtain an applicative functor, because you need to provide ap (i.e. <*> ) too, which cannot be defined in terms

Is there an instance of Monad but not of MonadFix?

随声附和 提交于 2019-12-06 18:42:57
问题 The question is mostly in the title. It seems like mfix can be defined for any monadic computation, even though it might diverge: mfix :: (a -> m a) -> m a mfix f = fix (join . liftM f) What is wrong with this construction? Also, why are the Monad and MonadFix typeclasses separate (i.e. what type has an instance of Monad but not of MonadFix )? 回答1: The left shrinking (or tightening) law says that mfix (\x -> a >>= \y -> f x y) = a >>= \y -> mfix (\x -> f x y) In particular this means that

Add to list if value is not null

≡放荡痞女 提交于 2019-12-06 17:32:15
问题 I have the function that could return null value: def func(arg: AnyRef): String = { ... } and I want to add the result to list, if it is not null: ... val l = func(o) if (l != null) list :+= l .... or def func(arg: AnyRef): Option[String] = { ... } ... func(o).filter(_ != null).map(f => list :+= f) ... But it looks too heavy. Are there any better solutions? 回答1: You can simply append the option to the list. This is because an Option can be treated as an Iterable (empty for None , with one