haskell

Concise way to conditionally update map in State monad

夙愿已清 提交于 2020-01-07 04:19:15
问题 Below is the code from an answer regarding memoization, showing a memoization function used in the State monad, where the state is updated with the result of the passed function if the key is not already in the map. type MyMemo a b = State (Map.Map a b) b myMemo :: Ord a => (a -> MyMemo a b) -> a -> MyMemo a b myMemo f x = do map <- get case Map.lookup x map of Just y -> return y Nothing -> do y <- f x modify $ \map' -> Map.insert x y map' return y It doesn't seem like idiomatic Haskell: it

haskell: recursive function that return the char in a tuple list with certain condition(compare)

纵然是瞬间 提交于 2020-01-07 04:03:27
问题 I'm learning recursive function in haskell that confused with such conditon: I got a tuple list here: [(0.5,'!'),(1,'*'),(1.5,'#')] What I want to do is input a number n and compare with fist number in each tuple of the list so suppose n=0.1, when it compared 0.5 and find it is smaller than 0.5, it will return char '!' suppose n=0.7, which is > 0.5 and keep comparing, find that it < 1, then it will return char '*' and after compare the whole list and find d is still bigger than the last one,

Type mismatch with Data.PSQueue

瘦欲@ 提交于 2020-01-07 04:00:49
问题 Eventually I want to achieve a simple in memory message queue that stores messages as key-tuples e.g. {Dst-IP, Dst-Port {CreationTime, MessageList}} where all future messages for a specific Destinations IP address and Destination Port should be appended to MessageList. I thought about invetigating Data.PSQueue (or maybe Data.Map). In the example above the Dst-IP, Dst-Port could be the key whilst CreationTime could be the priority. (I have no idea yet what the actual MessageList could be, but

parse error in nested if/do blocks [closed]

混江龙づ霸主 提交于 2020-01-07 03:47:05
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . defaultFileName :: [Char] defaultFileName = "Test.log" defaultSearchName :: String defaultSearchName = "xyz" This code can be compiled: a3 :: Int -> [[Char]] -> IO [Char] a3 index arg = if null arg then do a <- putStrLn "No parameters have been passed." a <- putStrLn $ "1 Default search string: " ++

Proving Composition Applicative law for ((->) r) type

余生长醉 提交于 2020-01-07 03:10:52
问题 The Composition Applicative Law is as follows: pure (.) <*> u <*> v <*> w = u <*> (v <*> w) Here's my attempt at proving the Composition law for the ((->) r) type: RHS: u <*> (v <*> w) u <*> ( \y -> v y (w y) ) \x -> u x ( (\y -> v y (w y)) x ) \x -> u x ( v x (w x)) -- (A) LHS: pure (.) <*> u <*> v <*> w const (.) <*> u <*> v <*> w (\f -> const (.) f (u f)) <*> v <*> w (\f -> (.) (u f)) <*> v <*> w (\g -> (\f -> (.) (u f)) g (v g)) <*> w \x -> (\g -> (\f -> (.) (u f)) g (v g)) x (w x) --

No instance for (Fractional a0) arising from a use of ‘it’

这一生的挚爱 提交于 2020-01-07 03:03:11
问题 Does anyone know why this code fails in GHCI? Prelude> let x = 4 in sum [(x^pow) / product[1.. pow] | pow <- [0.. 9]] <interactive>:70:1: No instance for (Fractional a0) arising from a use of ‘it’ The type variable ‘a0’ is ambiguous 回答1: Just use div: Prelude> let x = 4 in sum [(x^pow) `div` product[1.. pow] | pow <- [0.. 9]] 50 Notice the types of the operators: Prelude> :t (/) (/) :: Fractional a => a -> a -> a Prelude> :t div div :: Integral a => a -> a -> a / is for fractional numbers div

Ambiguous type using parameterized types Haskell

谁说我不能喝 提交于 2020-01-07 02:04:29
问题 I have a pretty straightforward function that takes a parameterized data type and returns the same type: {-# LANGUAGE ScopedTypeVariables #-} class IntegerAsType a where value :: a -> Integer newtype (Num a, IntegerAsType n) => PolyRing a n = PolyRing [a] deriving (Eq) normalize :: (Num a, IntegerAsType n) => (PolyRing a n) -> (PolyRing a n) normalize r@(PolyRing xs) | (genericLength xs) == len = r | ... [other cases] where len = (value (undefined :: n)) The idea is that normalize will take a

Ambiguous type using parameterized types Haskell

别等时光非礼了梦想. 提交于 2020-01-07 02:04:23
问题 I have a pretty straightforward function that takes a parameterized data type and returns the same type: {-# LANGUAGE ScopedTypeVariables #-} class IntegerAsType a where value :: a -> Integer newtype (Num a, IntegerAsType n) => PolyRing a n = PolyRing [a] deriving (Eq) normalize :: (Num a, IntegerAsType n) => (PolyRing a n) -> (PolyRing a n) normalize r@(PolyRing xs) | (genericLength xs) == len = r | ... [other cases] where len = (value (undefined :: n)) The idea is that normalize will take a

Unifying associated type synonyms with class constraints

牧云@^-^@ 提交于 2020-01-07 01:20:07
问题 I have a nested type that I want to partially specify using associated type synonyms. Below is some extremely reduced code that demonstrates the problem: {-# LANGUAGE TypeFamilies, FlexibleInstances, FlexibleContexts, MultiParamTypeClasses #-} f :: Int -> Int f x = x+1 data X t i newtype Z i = Z i deriving (Eq,Show) newtype Y q = Y (T q) class Qux m r where g :: (T m) -> r class (Integral (T a)) => Foo a where type T a -- ... functions instance (Integral i) => Foo (X (Z i) i) where type T (X

Get function as parameter in haskell

邮差的信 提交于 2020-01-06 23:00:23
问题 I can't figure this, I have a type called Enumeration > type Enumeration a = Int -> [a] And I need to map over it. I wrote this following function: > imapE :: (a -> b) -> Enumeration a -> Enumeration b > imapE f (m fa) = \n -> imapF f fa where imapF is defined like this: > imapF :: (a -> b) -> [a] -> [b] > imapF _ [] = [] > imapF f (x:xs) = f x : imapF f xs but when I try to load my code I get the following error BinaryTrees.lhs:91:14: Parse error in pattern: m regarding my imapE function. I