monads

Monad transformers: Implementation of a stack machine with MaybeT (State Stack)

。_饼干妹妹 提交于 2019-12-11 06:01:43
问题 I'm trying to implement a Maybe-State monad transformer and use it to implement a simple stack machine. The definitions of state monad and maybe should be correct. Now I'm trying to implement pop: pop :: MaybeT (State Stack) Int So that if the stack is empty it returns nothing, otherwise it returns Just <popped stack> . This is what I have so far: pop :: MaybeT (State Stack) Int pop = guard True (do (r:rs) <- get put rs return r) (Obviously True is just a dummy placeholder - I'll implement

How to use Control.Monad.Cont in a recursive function?

天涯浪子 提交于 2019-12-11 05:34:43
问题 I was providing an answer to this question and an idea came to me to use Cont monad. I don't know Haskell enough to explain why this program doesn't work import Control.Monad.Cont fib1 n = runCont (slow n) id where slow 0 = return 0 slow 1 = return 1 slow n = do a <- slow (n - 1) b <- slow (n - 2) return a + b main = do putStrLn $ show $ fib1 10 Error - main.hs:10:18: error: • Occurs check: cannot construct the infinite type: a2 ~ m a2 • In the second argument of ‘(+)’, namely ‘b’ In a stmt

unwrap getInputLine result from Input

旧巷老猫 提交于 2019-12-11 04:44:56
问题 I'm getting a result from getInputline , whose type is: (MonadException m) => IO String -> InputT m (Maybe String) I'd like to get just the Maybe String part. I'm well aware that in general there is no way to strip a monad, as explained in this answer (and other answers in the same question). However, since I'm doing it inside an InputT , I guess it's possible, as proposed here. However, I can't just use liftIO , as the answer suggests, since the IO is inside a StateT . loop :: Counter ->

Simple Haskell Monad - Random Number

守給你的承諾、 提交于 2019-12-11 04:09:21
问题 I'm trying to extend the code in this post (accepted answer) to allow me to be able to call randomGen2 to get a random number, based on the function randomGen which takes a seed as an argument. But everytime I call randomGen2, despite returning an Int, I get an error about not been able to print Random (when infact I am only trying to print Int). <interactive>:3:1: No instance for (Show (Random Int)) arising from a use of `print' Possible fix: add an instance declaration for (Show (Random Int

Haskell UI do clause, how to print?

允我心安 提交于 2019-12-11 03:48:38
问题 This is a follow up question to this. I'm using a graphic library in Haskell called Threepenny-GUI. In this library the main function returns a UI monad object. I'm trying to execute a simple print command with no success. What is a right work around to enable printing for debugging purposes. Code: main :: IO () main = startGUI defaultConfig setup setup :: Window -> UI () setup w = do print "debug message 1 " Error: Couldn't match type ‘IO’ with ‘UI’ Expected type: UI () Actual type: IO () In

Writing a custom map function

最后都变了- 提交于 2019-12-11 02:49:59
问题 Now there might be something in the Haskell Libraries to do what I want. I'm enough of a noob to not know any better and I'm trying to write a custom map function using the tools that I know. The type signature needs to be myMap :: (Monad m) => (a -> b) -> [m a] -> [m b] where myMap f as returns a list after applying f to each of the value in each Monad in as . My first attempt was myMap f = map (\x x >>= f) However, this has a type signature of myMap :: (Monad m) => (a -> m b) -> [m a] -> [m

Haskell: Graham Hutton Book Parsing (Ch-8): What does `parse (f v) out` do, and how does it do it?

浪子不回头ぞ 提交于 2019-12-11 02:43:45
问题 My question is about Graham Hutton's book Programming in Haskell 1st Ed. There is a parser created in section 8.4, and I am assuming anyone answering has the book or can see the link to slide 8 in the link above. A basic parser called item is described as: type Parser a = String -> [(a, String)] item :: Parser Char item = \inp -> case inp of [] -> [] (x:xs) -> [(x,xs)] which is used with do to define another parser p (the do parser) p :: Parser (Char, Char) p = do x <- item item y <- item

Matrix as Applicative functor, which is not Monad

允我心安 提交于 2019-12-11 02:36:09
问题 I run into examples of Applicatives that are not Monads. I like the multi-dimensional array example but I did not get it completely. Let's take a matrix M[A] . Could you show that M[A] is an Applicative but not a Monad with Scala code ? Do you have any "real-world" examples of using matrices as Applicatives ? 回答1: Something like M[T] <*> M[T => U] is applicative: val A = [[1,2],[1,2]] //let's assume such imaginary syntax for arrays val B = [[*2, *3], [*5, *2]] A <*> B === [[2,6],[5,4]] There

How to derive a state monad from first principles?

十年热恋 提交于 2019-12-11 02:23:07
问题 I am trying to come up with an implementation of State Monad derived from examples of function composition. Here I what I came up with: First deriving the concept of Monad: 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 -> Maybe' a inv' x = if x == 0 then Nothing' else Just' (1/x) log' :: (Floating a, Ord a) => a -> Maybe' a log' x = if x == 0 then Nothing'

Filtering and mixing monads in Slick for comprehension and Cats

筅森魡賤 提交于 2019-12-11 02:02:05
问题 I have the following objective: Create a monad that adds an user with the following computation flow: Check if an user exists with a specified e-mail, if he doesn't then : Check if the credentials given are ok (password long enough, etc.). If they are ok, then: Save the user to the DB My first "draft" would be something like this: val work: DBIO[UserId] = for { userO <- UserRepository.findByEmail(createdUser.email) //userO is Option[User] //This won't work cause Action.withFilter doesnt exist