monads

Haskell - Unable to define a State monad like function using a Monad like definition

我是研究僧i 提交于 2019-12-12 01:05:31
问题 I am trying to understand the concept of Monad by attempting to write generic version of functions that might be then include side effects to log, change state. Here is what I came up with: (The code is bit long, but it is there to show how I approached understanding monad - and this approach may not be correct) data Maybe' a = Nothing' | Just' a deriving Show sqrt' :: (Floating a, Ord a) => a -> Maybe' a sqrt' x = if x < 0 then Nothing' else Just' (sqrt x) inv' :: (Floating a, Ord a) => a ->

Use of unsafePerformIO appropriate?

荒凉一梦 提交于 2019-12-11 19:33:35
问题 Is using unsafePerformIO to allow read-only IO calls to non-changing files in pure code appropriate or is it going to cause a lot of problems? The main reason is because I'd like to store them in containers and for example, make them an instance of Ord , but I can't seem to imagine how to do that without wrapping IO calls in unsafePerformIO . 回答1: On safety Using unsafePerformIO in the way you describe should not cause any problems. The thumb rule is: if you are using unsafePerformIO to

Scala monads - “value map is not a member of” error

南楼画角 提交于 2019-12-11 18:29:52
问题 I have created a simple trait and service: @finalAlg @autoFunctorK(true) trait BettingService[F[_]] { def put(bet: Bet): F[Bet] } class BettingServiceMock[F[_] : Async] extends BettingService[F] { override def put(bet: Bet): F[Bet] = { val id = randomUUID().toString for { created <- Bet(BetId(id), bet.stake, bet.name) } yield created } } Bet and BetId are case classes : case class Bet(betId: BetId, stake: BigDecimal, name: String) case class BetId(betId: String) extends AnyVal When I ran this

Why can't a monad be decomposed?

徘徊边缘 提交于 2019-12-11 13:56:06
问题 I don't understand why I can't decompose a (let's say IO) monad. Like IO a -> a ? My question originated when using happstack and wanting to get the Text out of ServerPart (Maybe Text) which is returned by (optional $ lookText "domain") . Then I remembered reading that IO monad can't be escaped. I've read about unsafePerformIO and the reasons why it is bad, but none of those reasons seem to answer my question. 回答1: Can Monad s be escaped from? Yes. This is very easy with many Monad s, such as

How do I use a persistent State monad with Spock?

做~自己de王妃 提交于 2019-12-11 09:15:04
问题 I'm just starting out with haskell and I'm having issues with a basic "echo" REST server. Spock looked like a nice starting place for a REST server, and I though I got the basics of the State monad, but I'm having issues understanding how to put a runState around the spock code. Here's the code I've got so far. {-# LANGUAGE OverloadedStrings #-} module Main where import Data.Monoid import Web.Spock.Safe import qualified Control.Monad.State as S storeData :: String -> S.State String String

Using ReaderT transformer in ScottyT (vs ActionT)

孤街浪徒 提交于 2019-12-11 07:35:42
问题 I'm trying to thread configuration through my Scotty based application using ReaderT monad transformer approach, and having trouble doing so. I have to use configuration both when defining routes (as some of them depend on the config) and when handling actual requests. The latter works just fine in the ActionT, but no matter what I try I just can't get the types right in ScottyT. Here's the minimal example I compiled from the ReaderT sample from Scotty GitHub repository: {-# LANGUAGE

Use `seq` and print in haskell

此生再无相见时 提交于 2019-12-11 07:32:33
问题 I know that this is a little bit tricky but i wonder why it doesn't work! module Main where sillyDebug :: Int -> Int -> Int sillyDebug x y = (print x) `seq` (x + y) main :: IO () main = do print (sillyDebug 1 2) while its ideal is the same as sillyDebug = (trace (show x) False) `seq` (x + y) Is it related to lazy evaluation or side effect in haskell? https://hackhands.com/lazy-evaluation-works-haskell/ 回答1: Merely evaluating some IO action doesn’t do anything at all. You can think of IO sort

How to evaluate IO Bools in Haskell

心已入冬 提交于 2019-12-11 07:13:01
问题 I'm trying to write a function that takes an IO Bool and does stuff based on what this is, but I can't figure out how to evaluate the IO Bool. I tried saying do cond and do {cond==True} but got the error Couldn't match expected type 'Bool' against inferred type 'a b' . Can someone direct me to a solution? 回答1: You'll need to unpack/pull the bool out of IO before you can use it. Here's an example: main = useBool trueIO trueIO :: IO Bool trueIO = return True useBool :: IO Bool -> IO () useBool

Using a pure function in a Haskell monad / left-lifting?

*爱你&永不变心* 提交于 2019-12-11 06:28:02
问题 Consider the following function: foo = [1,2,3] >>= return . (*2) . (+1) For better readability and logic, I would like to move my pure functions (*2) and (+1) to the left of the return. I could achieve this like this: infixr 9 <. (<.) :: (a -> b) -> (b -> c) -> (a -> c) (<.) f g = g . f bar = [1,2,3] >>= (+1) <. (*2) <. return However, I don't like the right-associativity of (<.) . Let's introduce a function leftLift : leftLift :: Monad m => (a -> b) -> a -> m b leftLift f = return . f baz =

'do' construct in Haskell

杀马特。学长 韩版系。学妹 提交于 2019-12-11 06:20:54
问题 I'm trying to learn Haskell and want to write a small program which prints the content of a file to the screen. When I load it into GHCi I get the following error: The last statement in a 'do' construct must be an expression I know this question has be asked already here: Haskell — “The last statement in a 'do' construct must be an expression”. Even though my code is very similar I still can't figure out the problem. If anyone could point out the problem to me I'd be very thankful. module