haskell

Inconsistent Eq and Ord instances?

丶灬走出姿态 提交于 2019-12-30 17:45:40
问题 I have a large Haskell program which is running dismayingly slow. Profiling and testing has revealed that a large fraction of the time is spend comparing equality and ordering of a particular large datatype that is very important. Equality is a useful operation (this is state-space search, and graph search is much preferable to tree search), but I only need an Ord instance for this class in order to use Maps. So what I want to do is say instance Eq BigThing where (==) b b' = name b == name b'

Select random element from a set, faster than linear time (Haskell)

我与影子孤独终老i 提交于 2019-12-30 17:07:12
问题 I'd like to create this function, which selects a random element from a Set: randElem :: (RandomGen g) => Set a -> g -> (a, g) Simple listy implementations can be written. For example (code updated, verified working): import Data.Set as Set import System.Random (getStdGen, randomR, RandomGen) randElem :: (RandomGen g) => Set a -> g -> (a, g) randElem s g = (Set.toList s !! n, g') where (n, g') = randomR (0, Set.size s - 1) g -- simple test drive main = do g <- getStdGen print . fst $ randElem

Replace a 3 parameter list-comprehension by using map, concat

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 14:10:06
问题 I have some understanding of list comprehension. I understand that the expression: [x * x | x <- [1..10]] should output [1,4,9,16,25,36,49,64,81,100] and that the effect of that expression is the same as: map power [1..10] power x = x * x Now, I have to find out the other method (just like the above) for the following function: [(x,y+z) | x <- [1..10], y <- [1..x], z <- [1..y]] I can't figure it out by myself without errors, please help me 回答1: The Haskell Report tells us how to translate

Replace a 3 parameter list-comprehension by using map, concat

北城余情 提交于 2019-12-30 14:10:05
问题 I have some understanding of list comprehension. I understand that the expression: [x * x | x <- [1..10]] should output [1,4,9,16,25,36,49,64,81,100] and that the effect of that expression is the same as: map power [1..10] power x = x * x Now, I have to find out the other method (just like the above) for the following function: [(x,y+z) | x <- [1..10], y <- [1..x], z <- [1..y]] I can't figure it out by myself without errors, please help me 回答1: The Haskell Report tells us how to translate

Count occurrences of character in a string Haskell [closed]

痞子三分冷 提交于 2019-12-30 13:58:12
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . Trying to determine how to count the occurrences of a char is a string . I was it to be stored in a list [char,count] . countChars :: String -> [(Char, Int)] I'm new and learning Haskell so any help is much appreciated. 回答1: to give you a taste > (map (head &&& length) . group .

Unit testing IO actions with Hspec

你。 提交于 2019-12-30 11:31:50
问题 I have found other questions on similar lines but nothing that answers my question in this particular scenario. Furthermore, there seem to be few resources which succinctly cover the subject of unit testing IO actions in Haskell. Let's say I have this typeclass for my database communication: data Something = Something String deriving Show class MonadIO m => MonadDB m where getSomething :: String -> m Something getSomething s = do ... -- assume a DB call is made and an otherwise valid function

Unit testing IO actions with Hspec

南笙酒味 提交于 2019-12-30 11:31:17
问题 I have found other questions on similar lines but nothing that answers my question in this particular scenario. Furthermore, there seem to be few resources which succinctly cover the subject of unit testing IO actions in Haskell. Let's say I have this typeclass for my database communication: data Something = Something String deriving Show class MonadIO m => MonadDB m where getSomething :: String -> m Something getSomething s = do ... -- assume a DB call is made and an otherwise valid function

Why can't read deduce the correct type?

独自空忆成欢 提交于 2019-12-30 11:16:10
问题 In Haskell, I can make Haskell value from a string with read . Prelude> read "1" + 3 4 I can use fst to get the first element Prelude> fst (1,2) 1 However, I get an error when I combine read and fst to get the first element: Prelude> fst (read "(1,2)") <interactive>:20:6: Could not deduce (Read b0) arising from a use of ‘read’ from the context (Read a) bound by the inferred type of it :: Read a => a at <interactive>:20:1-18 The type variable ‘b0’ is ambiguous What's the problem? 回答1: As read

How to deliver JSON over HTTP using Warp with Aeson

拜拜、爱过 提交于 2019-12-30 10:55:08
问题 I want to create a high-performance HTTP-based API running on Haskell using warp as a HTTP backend. The server shall return JSON data upon request. This data shall be serialized by using Aeson However, warp requires a response object whereas Aeson returns lazy ByteString s. How can I tie both libraries together? For this question's scope I'm not interested in query parsing or routing, but in an example of how to tie both libraries together to deliver a correct JSON with correct headers. Note

Haskell int list to String

天涯浪子 提交于 2019-12-30 10:26:41
问题 I would like to know if there is a simple way to turn [5,2,10] into "52a" . Where its not just to this case, I want to associate any number >9 with the corresponding letter. Thanks in advance. 回答1: You want to do something to each element of a list in order to get a new list. In other words, you want to apply a function (that you will have to define yourself) to each element. This is what the map function from the Prelude is for. To convert between integers and individual characters, you