haskell

Is my understanding of a reducible expression i.e. redex correct?

浪尽此生 提交于 2019-12-24 08:25:59
问题 Programming in Haskell by Hutton says: An expression that has the form of a function applied to one or more arguments that can be ‘reduced’ by performing the application is called a reducible expression, or redex for short. Is a reducible expression i.e. redex exactly a function application where the function is not the result of another function application, equivalently, a function application where the function is either a function name or a lambda expression? Is either of the above two

How to parse array into tuple with aeson?

半世苍凉 提交于 2019-12-24 08:19:27
问题 If I have an array ["addTask", {"id": "1", "description": "d", "dependsOn": [], "dependentTasks": []}] . data Task = Task { id :: String , description :: String , dependsOn :: [String] , dependentTasks :: [String] } deriving (Eq, Show, Generic, ToJSON, FromJSON) type Change = Storage -> Storage addTask :: Task -> Change addTask (Task id desc dep dept) = insert id (Task id desc dep dept) How can I create a parser that would produce a addTask from that? instance FromJSON (Storage -> Storage)

Example of deep understanding of currying

北战南征 提交于 2019-12-24 08:17:21
问题 Reading https://wiki.haskell.org/Currying it states : Much of the time, currying can be ignored by the new programmer. The major advantage of considering all functions as curried is theoretical: formal proofs are easier when all functions are treated uniformly (one argument in, one result out). Having said that, there are Haskell idioms and techniques for which you need to understand currying. What is a Haskell technique/idiom that a deeper understanding of currying is required ? 回答1: Partial

Error binding type variables in instance of typeclass

牧云@^-^@ 提交于 2019-12-24 08:14:10
问题 I have a class "Shape" which should have "area" defined on all instances. area returns "Area b" (a data type) that contains a number (b belongs to Num typeclass) signifying the area of that Shape. Haskell has problem binding that b to (x*y) where x and y are of type 'a' and 'a' is also of typeclass Num. How do I solve this ?? [If i replace (x*y) by 0, it works but doesn't work even with (0::Int)] Code : data Unit = Unit | Meter | CentiMeter deriving Show data Area a = Area a Unit deriving

Handling Haskell zlib decompression errors

╄→гoц情女王★ 提交于 2019-12-24 07:39:39
问题 I have a String x which may or may not be gzip-compressed. Using the zlib library, I want to try decompressing x -- if it succeeds, the function shall return the compressed String. If not (i.e. x is not gzip-compressed) I want to simply return x . As GZip.decompress generates an error if applied to a non-gzip string, I could use catch or similar, but I'm specifically asking for a solution that uses the zlib error handling mechanism. How can I write a function, say decompressIfPossible ::

Type Errors when implementing Quadtree

与世无争的帅哥 提交于 2019-12-24 07:38:25
问题 Update: So this is my code: quadtreeToPic :: Quadtree -> Array (Int, Int) Word8 quadtreeToPic (QNode x y w avg Q0) | w == 1 = listArray (0,0) [avg] | w == 2 = listArray (0,4) [avg, avg, avg, avg] quadtreeToPic (QNode x y w avg (Q4 q1 q2 q3 q4)) = listArray ((0,0), (w-1,w-1)) (concat (map quadtreeToPic [q1, q2, q3, q4])) A Quadtree is either QNode Int Int Int Word8 QKids data QKids = Q0 | Q4 Quadtree Quadtree Quadtree Quadtree The error I get is Quadtree.hs:13:90: error: • Couldn't match type

How to simplify calling a field on a polymorphic field name into one typeclass

删除回忆录丶 提交于 2019-12-24 07:35:00
问题 In a previous question I asked how a record field can be made polymorphic when using DuplicateRecordFields. I got an excellent answer for this from @user2407038. He answered the question to my initial spec providing one type class per field, but he mentioned that it could all be simplified into one typeclass. (Note: this too can be generalized to a single class with an additional parameter corresponding to the field name; this is probably outside the scope of this question). I'm not sure how

Haskell/XMonad: What is the natural type for expressing that something must be done after a sequence of actions?

元气小坏坏 提交于 2019-12-24 07:28:40
问题 I have a sequence of X() actions during which certain buttons might be grabbed (and not released afterwards). In order to prevent buttons from ending up grabbed, I therefore have to ungrab every button at the end, for example: action1 >> action2 >> action3 >> ungrabAllButtons I wish to encode this requirement as a type, so that action1 , action2 , action3 can only be used if the buttons are ungrabbed afterwards. That is, even though action1 , action2 , are really X() actions, I would like

Stream transformer Monad for Vector.Stream

此生再无相见时 提交于 2019-12-24 07:26:50
问题 Data.Vector.Stream provides an nice Stream implementation that is very efficient thanks to the focus on fusability (see this paper for more). Since vector-0.1 this Stream implementation changed slightly by moving the Step type into a monad. (Now, the implementation is located in Data.Vector.Fusion.Stream.Monadic.) In a nutshell, here's the definition of Stream : data Step s a where Yield :: a -> s -> Step s a Skip :: s -> Step s a Done :: Step s a data Stream a = forall s. Stream (s -> Step s

Why are this two equivalents?

蹲街弑〆低调 提交于 2019-12-24 07:26:23
问题 I don't quite understand why given two list of lists xss :: [[a]] and yss :: [[a]] liftA2 (++) xss yss is equivalent to [xs ++ ys | xs <- xss, ys <- yss] 回答1: The reason is right here in the source code. instance Applicative [] where pure x = [x] fs <*> xs = [f x | f <- fs, x <- xs] liftA2 f xs ys = [f x y | x <- xs, y <- ys] The liftA2 definition is an optimization, and we could also do it manually with the default definition of liftA2 : liftA2 f x y = f <$> x <*> y So liftA2 (++) xs ys = (+