do-notation Questions
0
I translated the coroutine implementation from Control.Monad.Coroutine to Javascript. The translation might contain a few bugs, but my actual problem is that I don't know how to put everything toge...
Frech asked 7/12, 2021 at 20:12
2
I am learning Haskell.
I am trying to find elements of a list as which sum to elements of a list bs, returning the elements as a tuple:
findSum2 :: [Int] -> [Int] -> [(Int,Int,Int)]
findSum2 ...
Xylem asked 16/11, 2021 at 11:44
4
Solved
I want to write a simple game "guess number" - with n attempts. I want to add some conditions and hits. Is it possible to use guards inside do block ?
Here is my code:
game = return()
game n = do...
Tantalic asked 16/1, 2020 at 22:2
2
Solved
The docs state that "The simplest way to run a block where it cannot be a stand-alone statement is by writing do before it" and provide the following example:
# This dies half of the time...
Abigael asked 11/8, 2021 at 15:25
2
Solved
Does OCaml have an equivalent to Haskell's Do Notation?
Another way to put it - is there an easy way to handle nesting monadic operations more easily... cause this is annoying:
open Batteries
open ...
Affectation asked 26/5, 2021 at 6:20
2
Solved
Why is this function allowed:
-- function 1
myfunc :: String
myfunc = do
x <- (return True)
show x
and this is not:
-- function 2
myfunc :: String
myfunc = do
x <- getLine
show x
The co...
Head asked 9/10, 2020 at 13:57
1
Solved
Question is in bold at the bottom.
LYAH gives this example of using the do notation with the Writer monad
import Control.Monad.Writer
logNumber :: Int -> Writer [String] Int
logNumber x = write...
Ephemeris asked 3/6, 2020 at 20:19
1
Solved
I have the following sample of code:
{-# LANGUAGE ScopedTypeVariables #-}
main = do
putStrLn "Please input a number a: "
a :: Int <- readLn
print a
putStrLn "Please input a number b: "
b...
Tomfool asked 12/1, 2020 at 22:13
2
Solved
Consider this example:
{-# language ApplicativeDo #-}
module X where
data Tuple a b = Tuple a b deriving Show
instance Functor (Tuple a) where
fmap f (Tuple x y) = Tuple x (f y)
instance Fold...
Innocent asked 6/10, 2019 at 6:45
2
Solved
I want to achieve the following in a do block:
do
if condition then
n0 <- expr0
else
n0 <- expr0'
n1 <- expr1
n2 <- expr2
return T n0 n1 n2
But Haskell gives a compile error ...
Naif asked 1/9, 2019 at 19:4
3
Solved
I'm new to Haskell and functional programming and I was wondering why an example like this (the "nested loop") works:
do
a <- [1, 2, 3]
b <- [4, 5, 6]
return $ a * 10 + b
Some of the st...
Snappy asked 14/12, 2018 at 19:30
4
Solved
I'm trying to convert IO [String] to [String] with <- binding; however, I need to use a do block to do that under a where statement, but Haskell complains about the indentation all the time. Her...
Ender asked 11/6, 2018 at 18:51
3
Solved
To generate x86 assembly code, I have defined a custom type called X86:
data X86 a = X86 { code :: String, counter :: Integer, value :: (X86 a -> a) }
This type is used in do-notation like th...
Ephemerid asked 27/3, 2018 at 2:4
2
Solved
In java we always write:
public static void main(String[] args){...}
when we want to start writing a program.
My question is, is it the same for Haskell, IE: can I always be sure to declare:
ma...
Horatia asked 23/6, 2017 at 18:25
2
Solved
I have this type, basically a Kleisli arrow:
{-# language DeriveFunctor #-}
data Plan m i o = Plan (i -> m o) deriving Functor
instance (Monad m) => Applicative (Plan m i) where
pure x = ...
Tineid asked 18/12, 2016 at 16:47
4
Solved
Is there a "do notation" syntactic sugar for simple function composition?
(i.e. (.) :: (b -> c) -> (a -> b) -> a -> c)
I'd like to be able to store results of some compositions fo...
Crosshead asked 10/11, 2016 at 0:35
1
Solved
While reading the Haskell Wikibook about MonadPlus, I found the following function which basically takes a Char and a String and returns Just (char,tail) if such char is equal the string head, or N...
Karlenekarlens asked 11/10, 2016 at 1:1
3
Solved
I want to sequentially compose two monad actions in Haskell, discarding any value produced by the second, and passing the argument to both actions. Currently I'm using a do-block like this:
ask = ...
Ferity asked 8/12, 2015 at 17:20
1
Solved
Consider the following valid Haskell code
module Main where
main :: IO ()
main = do
let x = f
print x
f :: Maybe (Int, Int)
f =
Just 3 >>= (\a ->
Just 5 >>= (\b ->
return ...
Pliske asked 27/11, 2015 at 18:30
2
Solved
Working in haskell, found odd behavior, stripped it down to bare bones
This Works
a :: Bool
a = case True of
True -> True
False -> False
But when I try
b :: IO Bool
b = do
let b' = ca...
Nonrigid asked 8/10, 2015 at 0:12
2
Solved
The codes below looks quite clear:
do
x <- Just 3
y <- Just "!"
Just (show x ++ y)
Here the type of x is Num and y is String. (<- here is used to take actual value out of the Monad)...
Habitual asked 7/6, 2015 at 13:39
1
Solved
I've got some code that looks sort of like this, ignoring all the code that isn't relevant to my question:
import qualified Control.Monad.Reader as Reader
data FooEnv = FooEnv { bar :: Int ->...
Deannedeans asked 8/9, 2014 at 4:6
2
Solved
Say in a Haskell do-notation block, I wish to have a variable is_root indicating if I am root:
import System.Posix.User
main = do
uid <- getRealUserID
is_root <- return $ uid == 0
That a...
Diaghilev asked 14/8, 2014 at 18:11
2
Solved
I am still struggling with Haskell and now I have encountered a problem with wrapping my mind around the Input/Output monad from this example:
main = do
line <- getLine
if null line
then ret...
Citron asked 18/6, 2014 at 19:42
1
Solved
At HaskellWiki's Do notation considered harmful, section Useful applications, I found:
It shall be mentioned that the do sometimes takes the burden from you
to write boring things.
E.g. in ...
Aalst asked 26/2, 2014 at 21:59
1 Next >
© 2022 - 2024 — McMap. All rights reserved.