haskell

Data.STM.LinkedList implementation

拥有回忆 提交于 2020-06-17 02:27:48
问题 I'm looking at Data.STM.LinkedList implementation for a high performance linked list. Looking at the documentation, the length function run in O(n) - why is that ? Was there any real issue to implement it in O(1) ? Here is the source code https://hackage.haskell.org/package/stm-linkedlist-0.1.0.0/docs/src/Data-STM-LinkedList-Internal.html#length Is it possible implement it in O(1) ? i'm new to Haskell so I'm not sure if holding some metadata about the list is problematic. Thanks! 回答1: To a

Data.STM.LinkedList implementation

故事扮演 提交于 2020-06-17 02:26:46
问题 I'm looking at Data.STM.LinkedList implementation for a high performance linked list. Looking at the documentation, the length function run in O(n) - why is that ? Was there any real issue to implement it in O(1) ? Here is the source code https://hackage.haskell.org/package/stm-linkedlist-0.1.0.0/docs/src/Data-STM-LinkedList-Internal.html#length Is it possible implement it in O(1) ? i'm new to Haskell so I'm not sure if holding some metadata about the list is problematic. Thanks! 回答1: To a

How to convert Either to MonadThrow

一笑奈何 提交于 2020-06-16 07:18:30
问题 I have a function that handles errors via Either : funErrViaEither :: a -> Either SomeException b I want to use this function in another function that should be more flexible and return MonadThrow m : funErrViaThrow :: MonadThrow m => a -> m b funErrViaThrow x = if x = someCondition then funErrViaEither else throwM (SomeException MyCustomException) This does not compile; the type checker complains that the return type of funErrViaEither does not match the expected type m b . I don't

How to convert Either to MonadThrow

夙愿已清 提交于 2020-06-16 07:18:15
问题 I have a function that handles errors via Either : funErrViaEither :: a -> Either SomeException b I want to use this function in another function that should be more flexible and return MonadThrow m : funErrViaThrow :: MonadThrow m => a -> m b funErrViaThrow x = if x = someCondition then funErrViaEither else throwM (SomeException MyCustomException) This does not compile; the type checker complains that the return type of funErrViaEither does not match the expected type m b . I don't

How to convert precision in Image type from Double to Word8 using HIP?

两盒软妹~` 提交于 2020-06-16 03:37:26
问题 I have an image in the type Image VS Y Double (after using function readImageY from HIP library) and I want to convert it to Image VS Y Word8 . How should I go about doing this? I need to have it in this precision for the next function I am applying this to. Here's a snippet of the relevant code: import Codec.Picture import Codec.Picture.Types import Control.Arrow import Data.Ratio import Data.Monoid import Graphics.Image.Processing import qualified Graphics.Image as I import Graphics.Image

haskell running out of memory with finite lists

可紊 提交于 2020-06-15 07:09:09
问题 I run out of memory trying to run moderate inputs such as this: variation_models 15 25 also running higher numbers for ncars seems to make a huge difference in speed and memory usage. The slowdown is expected (there are more things to compare), but the exponential increase of memory usage doesn't make sense to me import Control.Monad orderedq f [] = True orderedq f (x:[]) = True orderedq f (x:y:zs) = f x y && orderedq f (y:zs) num_orderedq = orderedq (<=) adds_up_to n xs = n == sum xs both

haskell running out of memory with finite lists

流过昼夜 提交于 2020-06-15 07:09:06
问题 I run out of memory trying to run moderate inputs such as this: variation_models 15 25 also running higher numbers for ncars seems to make a huge difference in speed and memory usage. The slowdown is expected (there are more things to compare), but the exponential increase of memory usage doesn't make sense to me import Control.Monad orderedq f [] = True orderedq f (x:[]) = True orderedq f (x:y:zs) = f x y && orderedq f (y:zs) num_orderedq = orderedq (<=) adds_up_to n xs = n == sum xs both

What is a natural transformation in haskell?

匆匆过客 提交于 2020-06-14 05:06:59
问题 I would like to know, what is natural transformation in Haskell. The natural transformation is described with the following signature: F[a] ~> G[a] For example, I could transform: Maybe a ~> List a Right? What about IO , it is impossible to do natural transformation right? What is the purpose of the natural transformation? 回答1: A natural transformation, without getting into the category theory behind it, is really just a polymorphic function. Prelude> :set -XRankNTypes Prelude> :set

What is a natural transformation in haskell?

落爺英雄遲暮 提交于 2020-06-14 05:06:35
问题 I would like to know, what is natural transformation in Haskell. The natural transformation is described with the following signature: F[a] ~> G[a] For example, I could transform: Maybe a ~> List a Right? What about IO , it is impossible to do natural transformation right? What is the purpose of the natural transformation? 回答1: A natural transformation, without getting into the category theory behind it, is really just a polymorphic function. Prelude> :set -XRankNTypes Prelude> :set

Is there a difference between floor and truncate in Haskell

对着背影说爱祢 提交于 2020-06-13 18:24:41
问题 Does there exist a difference in functionality between floor and truncate in Haskell? They seem to perform the same functionality and they have the same type signature: truncate :: (Integral b, RealFrac a) => a -> b floor :: (Integral b, RealFrac a) => a -> b 回答1: Yes, for negative numbers. If we read the documentation, we see: truncate :: Integral b => a -> b truncate x returns the integer nearest x between zero and x and: floor :: Integral b => a -> b floor x returns the greatest integer