haskell

How to avoid stack space overflows?

僤鯓⒐⒋嵵緔 提交于 2020-01-12 03:58:17
问题 I've been a bit surprised by GHC throwing stack overflows if I'd need to get value of large list containing memory intensive elements. I did expected GHC has TCO so I'll never meet such situations. To most simplify the case look at the following straightforward implementations of functions returning Fibonacci numbers (taken from HaskellWiki). The goal is to display millionth number. import Data.List # elegant recursive definition fibs = 0 : 1 : zipWith (+) fibs (tail fibs) # a bit tricky

Replacing Exceptions With Either/Maybe/Option

 ̄綄美尐妖づ 提交于 2020-01-12 03:52:50
问题 I came across this dead end while trying to replace exceptions with either monad in c#. Which leads me to think maybe it is not only language specific problem and more technique related missing feature. Let me try to re-explain it more globally: Given: I have a 3rd party function( a function that is imported into my code and I have no access to) which receives a lazy list (c# IEnumerable,f# Seq...) and consume it I Want: To apply a function (LINQ select,map...) on the method's lazy list

Can Haskell's Control.Concurrent.Async.mapConcurrently have a limit?

廉价感情. 提交于 2020-01-12 03:23:10
问题 I'm attempting to run multiple downloads in parallel in Haskell, which I would normally just use the Control.Concurrent.Async.mapConcurrently function for. However, doing so opens ~3000 connections, which causes the web server to reject them all. Is it possible to accomplish the same task as mapConcurrently, but only have a limited number of connections open at a time (i.e. only 2 or 4 at a time)? 回答1: A quick solution would be to use a semaphore to restrict the number of concurrent actions.

How can I recover sharing in a GADT?

杀马特。学长 韩版系。学妹 提交于 2020-01-12 02:57:11
问题 In Type-Safe Observable Sharing in Haskell Andy Gill shows how to recover sharing that existed on the Haskell level, in a DSL. His solution is implemented in the data-reify package. Can this approach be modified to work with GADTs? For example, given this GADT: data Ast e where IntLit :: Int -> Ast Int Add :: Ast Int -> Ast Int -> Ast Int BoolLit :: Bool -> Ast Bool IfThenElse :: Ast Bool -> Ast e -> Ast e -> Ast e I'd like to recover sharing by transforming the above AST to type Name =

Adapting Error to Except

六眼飞鱼酱① 提交于 2020-01-12 01:39:32
问题 Now that Control.Monad.Error is deprecated, and Control.Monad.Except reigns supreme, a lot of sources online haven't caught up, and still show examples of how to use Error . So how would I go about turning instance Error MyError where noMsg = ... strMsg = ... into something using Except . Just replacing Error with Except didn't work as Except expects additional type parameters I understand that those exact methods don't exist in Except , so what's the alternative? 回答1: Short answer is:

Adapting Error to Except

末鹿安然 提交于 2020-01-12 01:39:08
问题 Now that Control.Monad.Error is deprecated, and Control.Monad.Except reigns supreme, a lot of sources online haven't caught up, and still show examples of how to use Error . So how would I go about turning instance Error MyError where noMsg = ... strMsg = ... into something using Except . Just replacing Error with Except didn't work as Except expects additional type parameters I understand that those exact methods don't exist in Except , so what's the alternative? 回答1: Short answer is:

Adapting Error to Except

夙愿已清 提交于 2020-01-12 01:38:52
问题 Now that Control.Monad.Error is deprecated, and Control.Monad.Except reigns supreme, a lot of sources online haven't caught up, and still show examples of how to use Error . So how would I go about turning instance Error MyError where noMsg = ... strMsg = ... into something using Except . Just replacing Error with Except didn't work as Except expects additional type parameters I understand that those exact methods don't exist in Except , so what's the alternative? 回答1: Short answer is:

How do you combine filter conditions

谁都会走 提交于 2020-01-11 21:21:03
问题 The filter class of functions takes a condition (a -> Bool) and applies it when filtering. What is the best way to use a filter on when you have multiple conditions? Used the applicative function liftA2 instead of liftM2 because I for some reason didn't understand how liftM2 worked within pure code. 回答1: The liftM2 combinator can be used in the Reader monad to do this in a 'more functional' way: import Control.Monad import Control.Monad.Reader -- .... filter (liftM2 (&&) odd (> 100)) [1..200]

How do you combine filter conditions

六月ゝ 毕业季﹏ 提交于 2020-01-11 21:20:03
问题 The filter class of functions takes a condition (a -> Bool) and applies it when filtering. What is the best way to use a filter on when you have multiple conditions? Used the applicative function liftA2 instead of liftM2 because I for some reason didn't understand how liftM2 worked within pure code. 回答1: The liftM2 combinator can be used in the Reader monad to do this in a 'more functional' way: import Control.Monad import Control.Monad.Reader -- .... filter (liftM2 (&&) odd (> 100)) [1..200]

How can I view the definition of a function in Haskell/GHCi?

拈花ヽ惹草 提交于 2020-01-11 18:04:15
问题 I'm using Haskell 2010.1.0.0.1 with GHC 6. Typing :t at the GHCi prompt followed by the name of a function shows us the type of the function. Is there a way to view the function definition as well? 回答1: Not currently. The closest command to what you want is :info :info name ... Displays information about the given name(s). For example, if name is a class, then the class methods and their types will be printed; if name is a type constructor, then its definition will be printed; if name is a