haskell

Is it possible to use a bracketing syntactic sugar for an applicative functor?

被刻印的时光 ゝ 提交于 2020-01-01 01:59:32
问题 In McBride and Paterson's 'Applicative programming with effects' they introduce some lovely syntactic sugar for lifting a pure function: [| f x y z |] for f <$> x <*> y <*> z and I recall someone somewhere else using li f w x y z il or il f v w x y z li , and I thought/hoped that might be because it could be defined using some existing language feature and cunning definition of li and il . I can't find any reference to this beyond the paper, and assuming that [| and |] aren't likely to turn

What is MonadBaseControl for?

我的未来我决定 提交于 2020-01-01 01:48:06
问题 I'm digging deeper into Yesod's monads, and have encountered MonadBaseControl . I took a look at the hackage doc, and got lost. Could someone tell me the problem it is trying to solve? 回答1: It comes from the package monad-control, and is one of a pair of type classes (the other one being MonadTransControl) that enhance MonadBase (resp. MonadTrans) by supporting an alternative liftBase (resp. lift ) operation for monads that implement it. This enhanced version no longer takes a simple action

Haskell pattern match “diverge” and ⊥

好久不见. 提交于 2020-01-01 01:41:06
问题 I'm trying to understand the Haskell 2010 Report section 3.17.2 "Informal Semantics of Pattern Matching". Most of it, relating to a pattern match succeeding or failing seems straightforward, however I'm having difficulty understanding the case which is described as the pattern match "diverging". I'm semi-persuaded it means that the match algorithm does not "converge" to an answer (hence the match function never returns). But if doesn't return, then, how can it return a value, as suggested by

Lens / Prism with error handling

狂风中的少年 提交于 2020-01-01 01:18:07
问题 Let's say I have a pair of conversion functions string2int :: String -> Maybe Int int2string :: Int -> String I could represent these fairly easily using Optics. stringIntPrism :: Prism String Int However if I want to represent failure reason, I'd need to keep these as two separate functions. string2int :: String -> Validation [ParseError] Int int2string :: Int -> String` For this simple example Maybe is perfectly fine, since we can always assume that a failure is a parse failure, thus we don

Round number to specified number of digits

余生颓废 提交于 2020-01-01 01:17:09
问题 Is there a simple function to round a Double or Float to a specified number of digits? I've searched here and on Hoogle (for (Fractional a) => Int -> a -> a ), but haven't found anything. 回答1: Not sure whether any standard function exists, but you can do it this way: (fromInteger $ round $ f * (10^n)) / (10.0^^n) 回答2: It depends on what you are going to do with the rounded number. If you want to use it in calculations, you should use Data.Decimal from Decimal library. If you want just to

Haskell “collections” language design

梦想的初衷 提交于 2020-01-01 01:17:09
问题 Why is the Haskell implementation so focused on linked lists? For example, I know Data.Sequence is more efficient with most of the list operations (except for the cons operation), and is used a lot; syntactically, though, it is "hardly supported". Haskell has put a lot of effort into functional abstractions, such as the Functor and the Foldable class, but their syntax is not compatible with that of the default list. If, in a project I want to optimize and replace my lists with sequences - or

How to cap memory usage of Haskell threads

寵の児 提交于 2020-01-01 01:16:31
问题 In a Haskell program compiled with GHC, is it possible to programmatically guard against excessive memory usage? That is, have it notify the program when memory usage reaches a specified limit, preferably indicating the offending thread. For example, suppose I want to write a server, hosting a scripting language interpreter, that users can connect to. It's Turing-complete, so programs could theoretically use unlimited memory or time. Suppose each client is handled with a separate thread. If a

Haskell Functor implied law

夙愿已清 提交于 2020-01-01 01:16:29
问题 Typeclassopedia says: "A similar argument also shows that any Functor instance satisfying the first law (fmap id = id) will automatically satisfy the second law as well. Practically, this means that only the first law needs to be checked (usually by a very straightforward induction) to ensure that a Functor instance is valid." If this is the case, why do we even mention the second functor law? Law 1: fmap id = id Law 2: fmap (g . h) = (fmap g) . (fmap h) 回答1: While I can't give a proof, I

How to cap memory usage of Haskell threads

耗尽温柔 提交于 2020-01-01 01:16:13
问题 In a Haskell program compiled with GHC, is it possible to programmatically guard against excessive memory usage? That is, have it notify the program when memory usage reaches a specified limit, preferably indicating the offending thread. For example, suppose I want to write a server, hosting a scripting language interpreter, that users can connect to. It's Turing-complete, so programs could theoretically use unlimited memory or time. Suppose each client is handled with a separate thread. If a

Recommended reading on general debugging techinques

女生的网名这么多〃 提交于 2019-12-31 22:32:29
问题 What reading would you recommend on general debugging techniques? I am more interested in principles and best practices than in specific platform solutions. For the record I mainly work with .NET (F#, C#), and dabble in Haskell and Ocaml. One of these Friday evenings we talked about debugging with my colleague on our walk home. I was surprised to learn that one can view and modify the state of live objects from the VisualStudio debugger. He also mentioned that another developer he knew, a