haskell

When is unsafeInterleaveIO unsafe?

我是研究僧i 提交于 2020-01-11 15:28:33
问题 Unlike other unsafe* operations, the documentation for unsafeInterleaveIO is not very clear about its possible pitfalls. So exactly when is it unsafe? I would like to know the condition for both parallel/concurrent and the single threaded usage. More specifically, are the two functions in the following code semantically equivalent? If not, when and how? joinIO :: IO a -> (a -> IO b) -> IO b joinIO a f = do !x <- a !x' <- f x return x' joinIO':: IO a -> (a -> IO b) -> IO b joinIO' a f = do !x

How can I generate different random values in Haskell?

拜拜、爱过 提交于 2020-01-11 13:22:08
问题 Suppose that I have a list like this: let list = ["random", "foo", "random", "bar", "random", "boo"] I want to iterate over a list and map all "random" elements to different random strings: let newList = fmap randomize list print newList -- ["dasidias", "foo", "gasekir", "bar", "nabblip", "boo"] My randomize function looks like this: randomize :: String -> String randomize str = case str of "random" -> randStr _ -> str where randStr = take 10 $ randomRs ('a','z') $ unsafePerformIO newStdGen

What are the space complexities of inits and tails?

大城市里の小女人 提交于 2020-01-11 09:23:09
问题 TL; DR After reading the passage about persistence in Okasaki's Purely Functional Data Structures and going over his illustrative examples about singly linked lists (which is how Haskell's lists are implemented), I was left wondering about the space complexities of Data.List 's inits and tails ... It seems to me that the space complexity of tails is linear in the length of its argument, and the space complexity of inits is quadratic in the length of its argument, but a simple benchmark

makeLenses for GADTs (Haskell)

巧了我就是萌 提交于 2020-01-11 08:29:09
问题 Is there an equivalent of makeLenses for GADTs? If I have a simple GADT like: data D a b where D :: (Ord a, Ord b) => !a -> !b -> D a b Is there a way to generate lenses automatically by passing in a constructor and a list of field names? 回答1: I don't think it can be done automatically, but writing some lenses by hand isn't that hard in this particular case: {-# LANGUAGE GADTs #-} import Control.Lens data D a b where D :: (Ord a, Ord b) => !a -> !b -> D a b field1 :: Lens' (D a b) a field1 f

Functions that look pure to callers but internally use mutation

ぐ巨炮叔叔 提交于 2020-01-11 08:27:09
问题 I just got my copy of Expert F# 2.0 and came across this statement, which somewhat surprised me: For example, when necessary, you can use side effects on private data structures allocated at the start of an algorithm and then discard these data structures before returning a result; the overall result is then effectively a side-effect-free function. One example of separation from the F# library is the library's implementation of List.map, which uses mutation internally; the writes occur on an

Haskell - Selectively Adding Lists

大憨熊 提交于 2020-01-11 07:41:30
问题 I have the following type: type Rating = (String, Int) type Film = (String, String, Int, [Rating]) testDatabase :: [Film] testDatabase = [("Director 1","Film 1",2012,[("TestRat",8)]),("Director 2","Film 2",2,[])] I need to find out what the average rating of the Director is based on all of their films combined and then all of their ratings combined. I genuinely have no idea how to approach this, I found it hard enough just to get the average of the tuples in the Film let alone work through

Haskell - Selectively Adding Lists

女生的网名这么多〃 提交于 2020-01-11 07:41:11
问题 I have the following type: type Rating = (String, Int) type Film = (String, String, Int, [Rating]) testDatabase :: [Film] testDatabase = [("Director 1","Film 1",2012,[("TestRat",8)]),("Director 2","Film 2",2,[])] I need to find out what the average rating of the Director is based on all of their films combined and then all of their ratings combined. I genuinely have no idea how to approach this, I found it hard enough just to get the average of the tuples in the Film let alone work through

replicate function for a length-indexed list using GHC.TypeLits and singletons

不打扰是莪最后的温柔 提交于 2020-01-11 07:34:08
问题 I'm trying to write a replicate function for a length-indexed list using the machinery from GHC.TypeLits, singletons, and constraints. The Vect type and signature for replicateVec are given below: data Vect :: Nat -> Type -> Type where VNil :: Vect 0 a VCons :: a -> Vect (n - 1) a -> Vect n a replicateVec :: forall n a. SNat n -> a -> Vect n a How can you write this replicateVec function? I have a version of replicateVec that compiles and type checks, but it appears to go into an infinite

replicate function for a length-indexed list using GHC.TypeLits and singletons

一个人想着一个人 提交于 2020-01-11 07:34:07
问题 I'm trying to write a replicate function for a length-indexed list using the machinery from GHC.TypeLits, singletons, and constraints. The Vect type and signature for replicateVec are given below: data Vect :: Nat -> Type -> Type where VNil :: Vect 0 a VCons :: a -> Vect (n - 1) a -> Vect n a replicateVec :: forall n a. SNat n -> a -> Vect n a How can you write this replicateVec function? I have a version of replicateVec that compiles and type checks, but it appears to go into an infinite

noob “Duplicate instance declarations” (again)

谁说我不能喝 提交于 2020-01-11 06:16:05
问题 yes...sorry this has been asked before, but usually about something so specific and complex that it in incomprehensible with a naïve OO head...we go.... class Animal a where class Mammal m where class Insect i where instance (Mammal m) => Animal m instance (Insect i) => Animal i and ghc goes Duplicate instance declarations: instance forall (k :: BOX) (m :: k). Mammal m => Animal m instance forall (k :: BOX) (i :: k). Insect i => Animal i and you look it up...and there is a solution using