haskell

WellPointed for dual to PreArrow class

流过昼夜 提交于 2021-01-28 03:42:20
问题 In Control.Arrow.Constrained there is a class WellPointed: Unlike with Morphism and PreArrow, a literal dual of WellPointed does not seem useful. Which is true as the duality for the following methods seems a bit weird: globalElement :: ObjectPoint a x => x -> a (UnitObject a) x unit :: CatTagged a (UnitObject a) const :: (Object a b, ObjectPoint a x) => x -> a b x But why would not we like (for example) having const for "choice" arrows (like having some constant error information which could

Haskell. update a single Vector element in O(1)

巧了我就是萌 提交于 2021-01-28 03:20:26
问题 I want to write a function that updates a single vector element in O(1): Vector Integer -> Int -> Integer -> Vector Integer upd v ind x It's easy to update a value copying the whole vector: upd v ind x = v // [(ind,x)] But that's way too slow. I create a vector as Data.Vector.Generic.fromList and do not freeze it. To modify the vector in-place I found function Data.Vector.modify , Data.Vector.Mutable.write and Data.Vector.Mutable.unsafeWrite , but I can't figure out how to use any of them.

How can I generate a random sequence of elements from a list in Haskell?

霸气de小男生 提交于 2021-01-28 02:50:34
问题 I've had a look through https://hackage.haskell.org/package/random-1.1/docs/System-Random.html however I can't see how to use a custom "list" for example an alphanumeric list of ['a'..'z'] ++ ['0' .. '9'] ? I suppose as a workaround I could instead map a random set of numbers instead. 回答1: The implementation of the work around I mentioned: Prelude> import System.Random Prelude System.Random> gen <- newStdGen Prelude System.Random> x = ['a'..'z'] ++ ['0' .. '9'] Prelude System.Random> fmap (x

Haskell: Install pureMD5 package

*爱你&永不变心* 提交于 2021-01-28 02:14:44
问题 I am trying to tell whether two files are likely the same, and found I could make a MD5 hash in Haskell from this StackOverflow thread: Compute MD5 digest of file in Haskell When I try to install pureMD5, I get an error: $ cabal install --lib pureMD5 Resolving dependencies... cabal: Could not resolve dependencies: [__0] trying: base-4.12.0.0/installed-4.1... (user goal) [__1] trying: ghc-8.6.5/installed-8.6... (user goal) [__2] next goal: process (user goal) [__2] rejecting: process-1.6.6.0

How can I generate a random sequence of elements from a list in Haskell?

不问归期 提交于 2021-01-27 23:43:31
问题 I've had a look through https://hackage.haskell.org/package/random-1.1/docs/System-Random.html however I can't see how to use a custom "list" for example an alphanumeric list of ['a'..'z'] ++ ['0' .. '9'] ? I suppose as a workaround I could instead map a random set of numbers instead. 回答1: The implementation of the work around I mentioned: Prelude> import System.Random Prelude System.Random> gen <- newStdGen Prelude System.Random> x = ['a'..'z'] ++ ['0' .. '9'] Prelude System.Random> fmap (x

How to skip elements in xml-conduit

早过忘川 提交于 2021-01-27 22:01:28
问题 I have to handle rather big XML files and I want to use the streaming API of xml-conduit to go through them and extract the info I need. In my case using streaming xml-conduit is especially appealing because I don't need much data from these files, and I need to perform simple aggregations on it so conduits are perfect. Now, I don't always know the exact structure of the file. Files are generated by different versions of (sometimes buggy) software around the world so I can't impose the schema

Constructing a n-ary product with all the values of a simple sum type

六月ゝ 毕业季﹏ 提交于 2021-01-27 21:14:10
问题 I'm working with the generics-sop library. I want to write a value with the following type: values :: forall r. IsEnumType r => NP (K r) (Code r) That is, for sum types whose constructors don't have any arguments (IsEnumType) I want to produce an n-ary product (NP) which contains the corresponding constructor value at each point. For example, for the type {-# LANGUAGE DeriveGeneric #-} import qualified GHC.Generics as GHC import Generics.SOP data Foo = Bar | Baz deriving (GHC.Generic)

Pattern bindings for existential constructors

ⅰ亾dé卋堺 提交于 2021-01-27 20:35:52
问题 While writing Haskell as a programmer that had exposure to Lisp before, something odd came to my attention, which I failed to understand. This compiles fine: {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE ExistentialQuantification #-} data Foo = forall a. Show a => Foo { getFoo :: a } showfoo :: Foo -> String showfoo Foo{getFoo} = do show getFoo whereas this fails: {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE ExistentialQuantification #-} data Foo = forall a. Show a => Foo { getFoo :: a }

Handling a collection of data in a Yesod Form

独自空忆成欢 提交于 2021-01-27 20:23:53
问题 Is it possible in Yesod to handle forms that contain a collection of data? I have a form that the user can add multiple people to, on the frontend it currently looks like this: { people.map((person, key) => ( <td> <input type="hidden" name={ `person[${key}][firstName]` } value={person.firstName} /> <input type="hidden" name={ `person[${key}][lastName]` } value={person.lastName} /> { person.firstName } { person.lastName } </td> )) } I then want to be able to translate that over to the backend

Could not find module `Control.Monad.State` after updating mtl

流过昼夜 提交于 2021-01-27 20:18:11
问题 I wanted to use the Control.Monad.Except module, but it turned out I had an outdated mtl package (It caused an import error - I had an obsolete module Control.Monad.Error ). So I did sudo cabal install mtl And it installed the 2.2.2 version. However, now I had two versions installed, 2.1.2 and 2.2.2 which still caused an import error. I followed instructions here and did sudo ghc-pkg unregister --force mtl-2.1.2 to remove the old version. But now I get an error: Could not find module `Control