haskell

How do I correctly use Control.Exception.catch in Haskell?

我与影子孤独终老i 提交于 2020-01-02 00:50:13
问题 Can someone please explain the difference between the behavior in ghci of the following to lines: catch (return $ head []) $ \(e :: SomeException) -> return "good message" returns "*** Exception: Prelude.head: empty list but catch (print $ head []) $ \(e :: SomeException) -> print "good message" returns "good message" Why isn't the first case catching the exception? Why are they different? And why does the first case put a double quote before the exception message? Thanks. 回答1: Let's examine

Why doesn't Safe Haskell support Template Haskell?

╄→尐↘猪︶ㄣ 提交于 2020-01-02 00:17:10
问题 The documentation for Safe Haskell states: [...] Unfortunately Template Haskell can be used to subvert module boundaries and so could be used gain access to this constructor. [...] The use of the -XSafe flag to compile the Danger module restricts the features of Haskell that can be used to a safe subset. This includes disallowing unsafePerfromIO, Template Haskell,[...] Used as a macro system that translates an AST to another AST, should it not be possible to simply restrict TH to the safe

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 do I write the qualified name of a symbol in Haskell?

六眼飞鱼酱① 提交于 2020-01-02 00:01:18
问题 I've got a name clash between two different Haskell modules that want to use the same infix operator ( <*> ). The Haskell 98 report says that modid.varsym is permitted, but I can't get it to work. In their entirety here are Test.hs : module Test where import qualified Test2 as T three = T.<*> and Test2.hs : module Test2 where (<*>) = 3 But trying to compile results in an error message: Test.hs:6:12: parse error on input `T.<*>' I tried T.(<*>) but that doesn't work either. How can I refer to

De Morgan's Laws in Haskell via the Curry-Howard Correspondence

此生再无相见时 提交于 2020-01-01 19:15:54
问题 I implemented three of the four De Morgan's Laws in Haskell: notAandNotB :: (a -> c, b -> c) -> Either a b -> c notAandNotB (f, g) (Left x) = f x notAandNotB (f, g) (Right y) = g y notAorB :: (Either a b -> c) -> (a -> c, b -> c) notAorB f = (f . Left, f . Right) notAorNotB :: Either (a -> c) (b -> c) -> (a, b) -> c notAorNotB (Left f) (x, y) = f x notAorNotB (Right g) (x, y) = g y However, I don't suppose that it's possible to implement the last law (which has two inhabitants): notAandBLeft

How to get make stats in constant memory

孤者浪人 提交于 2020-01-01 18:19:54
问题 I have a function, which creates some random numerical results. I know, that the result will be an integer in a (small, a - b approx 50) range a, b . I want to create a function which execute the above function let's say 1000000 times and calculates, how often the each result appears. (The function takes a random generator to produce the result.) The problem is, I don't know how to do this in constant memory without hard-coding the range's length. My (bad) approach is like this: values ::

Changing the return type of a function in Haskell?

北城余情 提交于 2020-01-01 16:51:14
问题 Is there is a succinct way to change the return type of a function? Consider for example comparing in Data.Ord . It returns an Ordering . Yet I am only interested in whether the two items are equal or not. So I need to convert the resulting Ordering to a Bool . A straight forward way I can think of is: isEqualOn f x y = if comparing f x y==EQ then True else False (or isEqualOn f x y = comparing f x y==EQ as here as pointed out in the comments). Is there a more compositional way to do this

Control thread to exit haskell application

断了今生、忘了曾经 提交于 2020-01-01 16:36:11
问题 I'm brand new to Haskell and in messing around with a few samples I've got a problem where I can't stop the program. I'm using Windows 7 and using runhaskell from ght. Ctrl-c doesn't work so I have to resort to the task manager which is a bit of a pain. Instead of doing that how can I create a separate control thread that would wait until I typed q and then quit my Haskell application. The application I've got the problem with is of the format: main = do h <- connectTo server (PortNumber

Control thread to exit haskell application

不羁的心 提交于 2020-01-01 16:36:06
问题 I'm brand new to Haskell and in messing around with a few samples I've got a problem where I can't stop the program. I'm using Windows 7 and using runhaskell from ght. Ctrl-c doesn't work so I have to resort to the task manager which is a bit of a pain. Instead of doing that how can I create a separate control thread that would wait until I typed q and then quit my Haskell application. The application I've got the problem with is of the format: main = do h <- connectTo server (PortNumber

What is the purpose of (const id) in this function?

北战南征 提交于 2020-01-01 14:21:50
问题 I'm trying to get deeper into the functional mindset and looking through solutions to exercises (99 problems). The first problem is to create a function that returns the last element of the list. I see the solution: myLast = foldr1 (const id) I understand that foldr1 applies a function f to a list l so if I plug it into an example: myLast [1,2,3,4,5,6,7] Which would be "translated to" foldr1 (const id) [1,2,3,4,5,6,7] Could someone explain to me what this (const id) is stepping through. I