haskell

Unwrapping a from IO (a)

一个人想着一个人 提交于 2021-01-28 07:31:50
问题 I've been learning Haksell the last 2 weeks and decided to try challenges at places such as Hackerrank. This has required learning IO. I have read many answers on stackExchange and the general gist is you don't unwrap IO a.. you just manipulate that data inside the IO function. That being the case what is the point of all the pure functions, if I'm not allowed to send data from main out to them? Here is some code that reads how many test cases, then for each test case reads N ordered pairs.

How to get Haskell Chart example1 working for Diagrams backend?

▼魔方 西西 提交于 2021-01-28 06:17:06
问题 The example outputs for Haskell's chart library look great. They all use the Cairo backend which I cannot install properly, so I'd like to try out the library using the Diagrams backend. However, it is not obvious to me how to do this. In particular, def is undefined. Can someone help me get started and show me how to modify the source code for example 1 so that it can run using the Diagrams backend? 回答1: The problematic def is the FileOptions argument to the Cairo backend's renderableToFile

How to write a Traversable instance for function, in Haskell?

痴心易碎 提交于 2021-01-28 05:28:12
问题 How do I write the Traversable instance for ((->) a) ? I think I could do it, if I could generically unwrap an Applicative Functor : instance Traversable ((->) k) where -- traverse :: (a -> f b) -> (k -> a) -> f (k -> b) -- traverse h t = ? -- h :: Applicative f => a -> f b -- t :: k -> a -- h . t :: k -> f b -- unwrap . h . t :: k -> b -- pure $ unwrap . h . t :: f (k -> b) traverse h t = pure $ unwrap . h . t unwrap :: (Functor f, Applicative f) => f a -> a unwrap y@(pure x) = x But, alas,

Is it possible to generate arbitrary functions in QuickCheck

…衆ロ難τιáo~ 提交于 2021-01-28 05:07:14
问题 I was trying to write a QuickCheck test for the identity f $ y = f y My initial plan was to write an arbitrary generator that returns functions & Integer, having the signature Gen (Int -> Int, Int) and in the prop_DollerDoesNothing test that function application with / without the $ gives the same result. This was my code: prop_DollarDoesNothing :: Property prop_DollarDoesNothing = forAll arbitraryFuncInt (\(f, y) -> (f $ y) == (f y)) arbitraryFuncInt :: Gen (Int -> Int, Int) arbitraryFuncInt

haskell elegant way to filter (reduce) sequences of duplicates from infinte list of numbers

馋奶兔 提交于 2021-01-28 05:04:59
问题 This is a function that produces an infinite list of random numbers import System.Random values :: [Int] values = map fst $ scanl (\(r, gen) _ -> randomR (1,10) gen) (randomR (1,10) (mkStdGen 1)) $ repeat () I want to reduce sequences for duplicate elements into one element e.g [2,3,4,1,7,7,7,3,4,1,1,1,3,..] -> [2,3,4,1,7,3,4,1,3,..] So, I need some elegant function "f" from [Int] -> [Int] that do this. Also, it must work with an infinite list lazily, so if I run f values it must not hang and

Is it possible to generate arbitrary functions in QuickCheck

不问归期 提交于 2021-01-28 05:03:06
问题 I was trying to write a QuickCheck test for the identity f $ y = f y My initial plan was to write an arbitrary generator that returns functions & Integer, having the signature Gen (Int -> Int, Int) and in the prop_DollerDoesNothing test that function application with / without the $ gives the same result. This was my code: prop_DollarDoesNothing :: Property prop_DollarDoesNothing = forAll arbitraryFuncInt (\(f, y) -> (f $ y) == (f y)) arbitraryFuncInt :: Gen (Int -> Int, Int) arbitraryFuncInt

Changing a do expression that uses pattern matching to application of the bind operator

我与影子孤独终老i 提交于 2021-01-28 05:02:39
问题 Original question LYAH, in For a Few Monads More shows this function, solveRPN :: String -> Maybe Double solveRPN st = do [result] <- foldM foldingFunction [] (words st) return result which uses pattern-matching in conjunction with do expression to esure that the monad coming out of foldM wraps a singleton list . In order to truly understand the nature of the do expression as well as of Monad , I have been rewriting most example from that book using >>= and >> instead of the do expression, a

haskell elegant way to filter (reduce) sequences of duplicates from infinte list of numbers

痴心易碎 提交于 2021-01-28 05:00:04
问题 This is a function that produces an infinite list of random numbers import System.Random values :: [Int] values = map fst $ scanl (\(r, gen) _ -> randomR (1,10) gen) (randomR (1,10) (mkStdGen 1)) $ repeat () I want to reduce sequences for duplicate elements into one element e.g [2,3,4,1,7,7,7,3,4,1,1,1,3,..] -> [2,3,4,1,7,3,4,1,3,..] So, I need some elegant function "f" from [Int] -> [Int] that do this. Also, it must work with an infinite list lazily, so if I run f values it must not hang and

Defining an “mempty”-like function with GHC Generics?

爱⌒轻易说出口 提交于 2021-01-28 04:44:45
问题 I am writing a client library for the Zoho REST API and have a bunch of different record types that have all Maybe a fields, i.e: data Approval = Approval { apDelegate :: Maybe Bool , apApprove :: Maybe Bool , apReject :: Maybe Bool , apResubmit :: Maybe Bool } deriving (Eq, Show, Generic) data ContactSpecialFields = ContactSpecialFields { csfCurrencySymbol :: Maybe Text -- $currency_symbol , csfState :: Maybe Text -- $state , csfProcessFlow :: Maybe Bool -- $process_flow , csfApproved ::

Non-Injective Closed Type Family

折月煮酒 提交于 2021-01-28 04:31:39
问题 I have this admittedly contrived chunk of code {-# LANGUAGE DataKinds, TypeFamilies #-} data Foo = Foo type family Id (n :: Foo) a where Id 'Foo a = a data Bar (n :: Foo) = Bar class Dispatch (n :: Foo) where consume :: Id n a -> Bar n -> a consume' :: Dispatch n => Id n [Bool] -> Bar n -> [Bool] consume' = consume consume'' :: Dispatch n => Id n [Bool] -> Bar n -> Bool consume'' g x = and (consume' g x) This compiles and works fine. However, if I replace the final consume'' definition with