haskell

ZipList Monoid haskell

隐身守侯 提交于 2021-02-04 07:29:08
问题 The default monoid for lists in the GHC Prelude is concatenation. [1,2,3] <> [4,5,6] becomes [1,2,3] ++ [4,5,6] and thus [1,2,3,4,5,6] I want to write a ZipList Monoid instance that behaves like this: [ 1 <> 4 , 2 <> 5 , 3 <> 6 ] The result is [5,7,9] assuming I am using the sum monoid. Note this behaves like zipWith (+) Potentially it would behave like this: [ Sum 1 <> Sum 4 , Sum 2 <> Sum 5 , Sum 3 <> Sum 6 ] I need to create a newtype around the ZipList newtype and the Sum newtype in order

Why can't Haskell be tricked into performing IO operations by using strict evaluation?

有些话、适合烂在心里 提交于 2021-02-04 03:18:28
问题 I'm just learning Haskell and IO monads. I'm wondering why wouldn't this force the program to output "hi" as well as "bye": second a b = b main = print ((second $! ((print "hi") >>= (\r -> return ()))) "bye") As far as I understand, the $! operator would force the first argument of second to be evaluated, and the >>= operator would need to run print "hi" in order to get a value off of it and pass it to \r -> return () , which would print "hi" to the screen. What's wrong with my reasoning? And

Why can't Haskell be tricked into performing IO operations by using strict evaluation?

人走茶凉 提交于 2021-02-04 03:05:14
问题 I'm just learning Haskell and IO monads. I'm wondering why wouldn't this force the program to output "hi" as well as "bye": second a b = b main = print ((second $! ((print "hi") >>= (\r -> return ()))) "bye") As far as I understand, the $! operator would force the first argument of second to be evaluated, and the >>= operator would need to run print "hi" in order to get a value off of it and pass it to \r -> return () , which would print "hi" to the screen. What's wrong with my reasoning? And

Haskell quickBatch: Testing ZipList Monoid at mconcat results in stack overflow

我是研究僧i 提交于 2021-02-02 09:31:29
问题 i've created orphaned instances for ZipList Semigroup and Monoid. However, when I run the tests from quickBatch on monoid, at the mconcat test, there is a stack overflow error. How do I resolve this error? Why is there such an error? Is it due to pure mempty , which I do not quite understand as I got this mostly from HaskellBook Chapter 17 Applicative section 17.8 ZipList Monoid? zl :: ZipList (Sum Int) zl = ZipList [1,1 :: Sum Int] instance Semigroup a => Semigroup (ZipList a) where (<>) =

Why is the strictness-introducing function called seq?

ぐ巨炮叔叔 提交于 2021-02-02 00:30:44
问题 I understand the seq function and why it's necessary to introduce strictness for efficiency. What I don't understand is, why is this primitive called seq (and not something to do with strictness)? 回答1: TL;DR: Miranda called it seq , it was introduced when sequence was (probably) already a thing for Monads, and ($!) was known as strict for a short time. Miranda was first It is called seq because it was called seq in Miranda and previous languages, at least according to A History of Haskell:

Why is the strictness-introducing function called seq?

怎甘沉沦 提交于 2021-02-02 00:29:52
问题 I understand the seq function and why it's necessary to introduce strictness for efficiency. What I don't understand is, why is this primitive called seq (and not something to do with strictness)? 回答1: TL;DR: Miranda called it seq , it was introduced when sequence was (probably) already a thing for Monads, and ($!) was known as strict for a short time. Miranda was first It is called seq because it was called seq in Miranda and previous languages, at least according to A History of Haskell:

Why is the strictness-introducing function called seq?

杀马特。学长 韩版系。学妹 提交于 2021-02-02 00:29:51
问题 I understand the seq function and why it's necessary to introduce strictness for efficiency. What I don't understand is, why is this primitive called seq (and not something to do with strictness)? 回答1: TL;DR: Miranda called it seq , it was introduced when sequence was (probably) already a thing for Monads, and ($!) was known as strict for a short time. Miranda was first It is called seq because it was called seq in Miranda and previous languages, at least according to A History of Haskell:

Why is the strictness-introducing function called seq?

家住魔仙堡 提交于 2021-02-02 00:29:28
问题 I understand the seq function and why it's necessary to introduce strictness for efficiency. What I don't understand is, why is this primitive called seq (and not something to do with strictness)? 回答1: TL;DR: Miranda called it seq , it was introduced when sequence was (probably) already a thing for Monads, and ($!) was known as strict for a short time. Miranda was first It is called seq because it was called seq in Miranda and previous languages, at least according to A History of Haskell:

Why is the strictness-introducing function called seq?

十年热恋 提交于 2021-02-02 00:28:13
问题 I understand the seq function and why it's necessary to introduce strictness for efficiency. What I don't understand is, why is this primitive called seq (and not something to do with strictness)? 回答1: TL;DR: Miranda called it seq , it was introduced when sequence was (probably) already a thing for Monads, and ($!) was known as strict for a short time. Miranda was first It is called seq because it was called seq in Miranda and previous languages, at least according to A History of Haskell:

Is there a way to place some impure code inside pure functions?

試著忘記壹切 提交于 2021-02-01 05:20:34
问题 IO , just like Maybe , is just an instance of Monad . On the other hand we have all data constructors for Maybe ( Just and Nothing ), but no constructors for IO . Reader and Writer do not export constructors too, they have functions, which return instance of this type ( reader and writer ) and more importantly runReader and runWriter , which unwrap computation result from Monad. Is there a way to unwrap IO Monad? I would like to have pure function which do some impure IO computations under