haskell

How to combine two different monads

拟墨画扇 提交于 2020-01-04 05:21:20
问题 I'm testing a REST server. I hit it in the IO monad and simulate it in State Db where Db tracks the supposed state of the server. The following function is supposed to run both versions and compare the results... check :: (Eq a, MonadState d s) => s a -> IO a -> s (IO Bool) -- or: check :: (Eq a, MonadState d s, MonadIO i) => s a -> i a -> s (i Bool) check _ _ = (return.return) False -- for now but when I try it with these simplest possible functions ... simReset :: State Db () realReset ::

Pattern matching against monadic result?

点点圈 提交于 2020-01-04 05:12:11
问题 I am learning Haskell and want to use "readHex", which according to Hoogle has type: readHex :: Num a => ReadS a How do you "extract" a result from such a function? What's the most common way, pattern match against the right constructor ie, [(a,"")] ?? LiftM and lifting in general seems to make some sense, but I'm lost when it comes to "unwinding" the monadic stack. 回答1: To answer the general question in general terms, the only way to extract values from a data constructor is pattern matching

Render url with query parameters

我只是一个虾纸丫 提交于 2020-01-04 04:44:09
问题 Can't find solution to a simple problem, the answer should be obvious. How to render url with a query parameters inside hamlet template? I.e. @{ItemsR} will generate http://localhost:3000/items and how do I generate something like http://localhost:3000/items?page=10&sort=name ? 回答1: Yesod is RESTful, you should use arguments in url format (eg. /items/page/10/sortby/name ) if you wish use QueryString format then, you loss the Yesod type safe url management. Below example show how use different

multiSelectField with pre-selected options in Yesod

扶醉桌前 提交于 2020-01-04 04:40:27
问题 I have a many-to-many relationship between two entities. For example, let's think about a blogpost which can have multiple authors. This could be a simple model for it: share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| Person name String age Int Maybe deriving Show Blogpost title String deriving Show PersonBlogpost person PersonId blogpost BlogpostId |] My idea is to have an "Edit" page for a Blogpost entity, and in it have a multiSelectField for the authors, and have

Dont understand whats on in this haskell code

為{幸葍}努か 提交于 2020-01-04 04:34:07
问题 I have some haskell code Im trying to work my way thourgh but I dont have understand what is going in it. type Bag a = a -> Int emptyB :: Bag a emptyB = \e -> 0 countB :: Eq a => Bag a -> a -> Int countB b e = b e I understand that the Bag type is a function that takes in a generic object and returns a Int and countB is basically a wrapper for Bag that gets the number of generic objects in that Bag. But I dont really understand anything past that. How do I modify whats in the bag? Or the bag

Why it does not show as evaluated? [duplicate]

落爺英雄遲暮 提交于 2020-01-04 04:22:12
问题 This question already has an answer here : :sprint for polymorphic values? (1 answer) Closed 11 months ago . I am trying to figure out, how lazy evaluation works and I've tried as following: Prelude> a = [1,2,3,5,6] Prelude> b = map (\x -> x * 8) a Prelude> :sprint b b = _ Prelude> b [8,16,24,40,48] Prelude> :sprint b b = _ The question is, why the last line does not show the evaluated list? I evaluated a line before. 回答1: Because b is basically a function from Num a dictionary to [a] for

Reading from a handle obtained outside createProcess

允我心安 提交于 2020-01-04 04:10:15
问题 I'm trying to create a process, and communicate with it via a handle that I provide outside the createProcess function: stdOutH <- openFile (logDir </> "stdout.log") ReadWriteMode hSetBuffering stdOutH LineBuffering (_, _, _, ph) <- createProcess $ (proc "someproc" []) { std_out = UseHandle stdOutH , std_err = UseHandle stdErrH } line <- hGetLine stdOutH putStrLn $ "Got " ++ line The "someproc" process spits a line out to the standard output, and I want to read it from the process that

Function to Calculate `log` of Integer

ぐ巨炮叔叔 提交于 2020-01-04 04:10:10
问题 I wrote the following function. f :: Integer -> Integer f x = if (odd x) then 0 else (floor . logBase 2) x But the following compile-time error occurs: F.hs:2:31: No instance for (RealFrac Integer) arising from a use of floor' Possible fix: add an instance declaration for (RealFrac Integer) In the first argument of (.)', namely `floor' In the expression: floor . logBase 2 In the expression: (floor . logBase 2) x F.hs:2:39: No instance for (Floating Integer) arising from a use of logBase'

Why is “(2+2.0)” Double in a .hs file but “Fractional a => a” in GHCi?

徘徊边缘 提交于 2020-01-04 04:08:25
问题 Loading a = 2+2.0 from a .hs file in GHCi and doing :t a shows a :: Double . On the other hand, doing let b = 2+2.0 and :t b in GHCi shows b :: Fractional a => a . How are you able to deduce this from these two documents? 4.3.4 Ambiguous Types, and Defaults for Overloaded Numeric Operations https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-750004.3 2.4.8 Type defaulting in GHCi: https://downloads.haskell.org/~ghc/7.8.3/docs/html/users_guide/interactive-evaluation.html I

“packageName” with GHC.Generics

不想你离开。 提交于 2020-01-04 04:02:11
问题 I have a class that provides a globally unique identifier for types: class Named a where nameOf :: a -> (String,String,String) -- (Package, Module, Identifier) default nameOf :: (Generic a, Named' (Rep a)) => a -> (String,String,String) nameOf = nameOf' . from which almost works: >>> data D = C >>> instance Named D >>> nameOf C ("","Main","D") but I can't get the datatype's package with GHC.Generics : class Named' f where nameOf' :: f a -> (String,String,String) instance (Datatype t) => Named