monads

Haskell: I/O and Returning From a Function

最后都变了- 提交于 2020-01-02 03:29:08
问题 Please bear with me as I am very new to functional programming and Haskell. I am attempting to write a function in Haskell that takes a list of Integers, prints the head of said list, and then returns the tail of the list. The function needs to be of type [Integer] -> [Integer]. To give a bit of context, I am writing an interpreter and this function is called when its respective command is looked up in an associative list (key is the command, value is the function). Here is the code I have

building Either (or Result) on top of Choice in F#

若如初见. 提交于 2020-01-02 02:46:09
问题 I built a monad for success/failure based on information in Scott Wlaschin's blog with extra help from this stack overflow posting. I ended up with a type type Result<'a> = | Success of 'a | Error of string I now realize that this is equivalent of Choice in F#, and Either in haskell. I'd like to reuse code instead of keeping my own, but would like to just change the implementation and not have to change my existing code. I'd like to use my existing names along with the implementation of

Does Haskell have foldlM'?

心不动则不痛 提交于 2020-01-02 02:02:24
问题 How does one fold over a monad strictly? Data.Foldable has the strict foldl' and the monadic foldlM , but no strict foldlM' ? Is the strictness somehow defined by the monad itself? If so, how does one work out what it is? Imagine I must determine whether the product of an enormous list of ring elements is zero, but my ring isn't an integral domain, i.e. it contains zero devisors. In this case, I should tail recursively foldl my multiplication *** over the list, but return False the moment the

Haskell — dual personality IO / ST monad?

微笑、不失礼 提交于 2020-01-02 00:12:09
问题 I have some code that currently uses a ST monad for evaluation. I like not putting IO everywhere because the runST method produces a pure result, and indicates that such result is safe to call (versus unsafePerformIO ). However, as some of my code has gotten longer, I do want to put debugging print statements in. Is there any class that provides a dual-personality monad [or typeclass machinery], one which can be a ST or an IO (depending on its type or a "isDebug" flag)? I recall SPJ

How to reduce iterations when chaining map reduce filter?

无人久伴 提交于 2020-01-01 18:37:07
问题 I have been reading about map , reduce and filter a lot because of how much they are used in react and FP in general. If we write something like: let myArr = [1,2,3,4,5,6,7,8,9] let sumOfDoubleOfOddNumbers = myArr.filter(num => num % 2) .map(num => num * 2) .reduce((acc, currVal) => acc + currVal, 0); 3 different loops are run. I've read about Java 8 streams as well and know that they use what is called a monad, ie, the computations are stored first. They are performed once only in one

Scala Option object inside another Option object

大城市里の小女人 提交于 2020-01-01 05:09:08
问题 I have a model, which has some Option fields, which contain another Option fields. For example: case class First(second: Option[Second], name: Option[String]) case class Second(third: Option[Third], title: Option[String]) case class Third(numberOfSmth: Option[Int]) I'm receiving this data from external JSON's and sometimes this data may contain null's, that was the reason of such model design. So the question is: what is the best way to get a deepest field? First.get.second.get.third.get

runST with Hindley-Milner type system

霸气de小男生 提交于 2020-01-01 04:42:05
问题 If I understand the ST monad in Haskell correctly, runST uses rank-2 types in a clever way to ensure that a computation does not reference any other thread when escaping the monad. I have a toy language with a Hindley-Milner type system, and my question is the following: is it possible to extend the HM type system with an ad-hoc rule for typing runST applications so that the ST monad is safely escapable, without introducing rank-2 types? More precisely, runST would have type forall s a. ST s

Is there any intuition to understand join two functions in Monad?

前提是你 提交于 2020-01-01 04:34:07
问题 join is defined along with bind to flatten the combined data structure into single structure. From type system view, (+) 7 :: Num a => a -> a could be considered as a Functor , (+) :: Num a => a -> a -> a could be considered as a Functor of Functor , how to get some intuition about it instead of just relying on type system? Why join (+) 7 === 14 ? Even though it is possible to get the final result through manually stepping by the function binding process, it would be great if some intuition

Mix and match stateful computations within the State monad

独自空忆成欢 提交于 2020-01-01 04:15:06
问题 The state of my program consists of three values, a , b , and c , of types A , B , and C . Different functions need access to different values. I want to write functions using the State monad so that each function can only access the pieces of the state that it needs to access. I have four functions of the following types: f :: State (A, B, C) x g :: y -> State (A, B) x h :: y -> State (B, C) x i :: y -> State (A, C) x Here is how I call g within f : f = do -- some stuff -- y is bound to an

Experience reports using indexed monads in production?

萝らか妹 提交于 2020-01-01 02:48:06
问题 In a previous question I discovered the existence of Conor McBride's Kleisli arrows of Outrageous Fortune while looking for ways of encoding Idris examples in Haskell. My efforts to understand McBride's code and make it compile in Haskell led to this gist: https://gist.github.com/abailly/02dcc04b23d4c607f33dca20021bcd2f While searching on Hackage, I discovered several implementations of these concepts, notably by (guess who?) Edward Kmett and Gabriel Gonzalez. What experience do people have