haskell

Expressive and composable error types

拈花ヽ惹草 提交于 2021-02-06 00:49:03
问题 I am struggling with the best way to report errors in a set of functions that should compose nicely, in a library I'm working on. Concretely, I have functions that look like: foo, bar, baz :: a -> Maybe a where foo can fail in only one way (a good fit for Maybe ), but bar and baz can fail in two different ways each (good fits for Either BarErrors and Either BazErrors ). One solution is to create: data AllTheErrors = TheFooError | BarOutOfBeer | BarBurnedDown | ... and make all the functions

Expressive and composable error types

做~自己de王妃 提交于 2021-02-06 00:48:52
问题 I am struggling with the best way to report errors in a set of functions that should compose nicely, in a library I'm working on. Concretely, I have functions that look like: foo, bar, baz :: a -> Maybe a where foo can fail in only one way (a good fit for Maybe ), but bar and baz can fail in two different ways each (good fits for Either BarErrors and Either BazErrors ). One solution is to create: data AllTheErrors = TheFooError | BarOutOfBeer | BarBurnedDown | ... and make all the functions

Haskell - Counting how many times each distinct element in a list occurs

夙愿已清 提交于 2021-02-05 20:27:37
问题 I'm new to Haskell and am just trying to write a list comprehension to calculate the frequency of each distinct value in a list, but I'm having trouble with the last part.. So far i have this: frequency :: Eq a => [a] -> [(Int,a)] frequency list = [(count y list,y) | y <- rmdups ] Something is wrong with the last part involving rmdups. The count function takes a character and then a list of characters and tells you how often that character occurs, the code is as follows.. count :: Eq a => a -

Haskell - Counting how many times each distinct element in a list occurs

无人久伴 提交于 2021-02-05 20:26:51
问题 I'm new to Haskell and am just trying to write a list comprehension to calculate the frequency of each distinct value in a list, but I'm having trouble with the last part.. So far i have this: frequency :: Eq a => [a] -> [(Int,a)] frequency list = [(count y list,y) | y <- rmdups ] Something is wrong with the last part involving rmdups. The count function takes a character and then a list of characters and tells you how often that character occurs, the code is as follows.. count :: Eq a => a -

Is Milner let polymorphism a rank 2 feature?

↘锁芯ラ 提交于 2021-02-05 13:10:27
问题 let a = b in c can be thought as a syntactic sugar for (\a -> c) b , but in a typed setting in general it's not the case. For example, in the Milner calculus let a = \x -> x in (a True, a 1) is typable, but seemingly equivalent (\a -> (a True, a 1)) (\x -> x) is not. However, the latter is typable in System F with a rank 2 type for the first lambda. My questions are: Is let polymorphism a rank 2 feature that sneaked secretly in the otherwise rank 1 world of Milner calculus? The purpose of

Haskell - creating a function definition for the function `all` using foldr

旧巷老猫 提交于 2021-02-05 12:30:55
问题 I'm trying to create a function definition for the function all using foldr . p is the predicate. I know this can be done: all p = and . foldr (\x xs -> p x : xs) [] But what I want to do is to shift the function and into the foldr equation. Can this be done? I've tried the following, which all failed to work: all p = foldr (\x p -> \ys -> and (p x) ys) True all p = foldr (\x and -> (\ys -> (p x and ys))) True all p = foldr (\x ys -> and . (p x) ys) True Am I falling short in my understanding

How do we partition list in Haskell by a separator element of the list?

若如初见. 提交于 2021-02-05 12:24:14
问题 I want to take an Int value and a list that returns a list of lists where the Int value is what separates the values into groups. Here is an example below: partitionBy :: Eq a => a -> [a] -> [[a]] partitionBy = undefined -- pending implementation Example: partitionBy 0 [1,2,3,0,4,0,2,9,1] == [[1,2,3],[4],[2,9,1]] 回答1: Assuming every separator ( sep ) value encountered at the start or end, as well as in succession would create an empty list, here's what I came up with: partitionBy :: Eq a => a

How do we partition list in Haskell by a separator element of the list?

耗尽温柔 提交于 2021-02-05 12:22:41
问题 I want to take an Int value and a list that returns a list of lists where the Int value is what separates the values into groups. Here is an example below: partitionBy :: Eq a => a -> [a] -> [[a]] partitionBy = undefined -- pending implementation Example: partitionBy 0 [1,2,3,0,4,0,2,9,1] == [[1,2,3],[4],[2,9,1]] 回答1: Assuming every separator ( sep ) value encountered at the start or end, as well as in succession would create an empty list, here's what I came up with: partitionBy :: Eq a => a

Haskell quickBatch testing: Applicative Monoid ZipList

北慕城南 提交于 2021-02-05 11:41:37
问题 I'm trying to do a quickBatch test on the solution provided at ZipList Monoid haskell. I'll need some advice as to how to continue from here or should I try something else for EqProp (Ap f a) ? How do I go about deriving a solution for this? newtype Ap f a = Ap { getAp :: f a } deriving (Eq, Show) instance (Applicative f, Semigroup a) => Semigroup (Ap f a) where Ap xs <> Ap ys = Ap $ liftA2 (<>) xs ys instance (Applicative f, Monoid a) => Monoid (Ap f a) where mempty = Ap $ pure mempty Ap xs

Another question about random numbers in Haskell

折月煮酒 提交于 2021-02-05 11:21:11
问题 I am trying to make a version of the Voltorb game from Pokemon Gold and Silver in Haskell. Now for generation of the board, I want to have a list of (l,r,v) triplets where l is the line, r is the row and v is the value of the field. Values l and r are implemented with list comprehension since they should be the same every time. As for v though I can't find an option to implement it so that it is 0,1,2 or 3 "randomly" (I know that Haskell is purely functional and there is no true randomness,