Chaining State Monad
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a function
step :: Int -> State Int Int
step n = get >>= x -> put (x `div` n) >> return (x `mod` n)
λ> runState (step 25) 41
(16,1)
How do I run a sequence of steps, with different values of n and each step using the state from the last step?
so for example the steps would be as follows
step one yields (16,1) which I then want to use as input for my next step with n = 10 which should yield (6, 2). With the 1 from the first step being added to and the 16 in step one being mod with the new n.
n = 25 gives (16,1) then
n = 10 gives (6,2) then
n = 5 gives (1,3) then
n = 1 gives (0,4)
I am aware that using State here might not be correct; but I am trying to use it as a way to learn.
may aim is to implement this function with the state monad.
greedy :: Double -> Int
greedy owed = snd $ foldl go (pennies,0) [25, 10, 5, 1]
where
pennies = floor . (*100) $ owed
go (remaining,counter) coin = (remaining',counter')
where
remaining' = remaining `mod` coin
counter' = counter + remaining `div` coin
haskell monads state-monad
add a comment |
I have a function
step :: Int -> State Int Int
step n = get >>= x -> put (x `div` n) >> return (x `mod` n)
λ> runState (step 25) 41
(16,1)
How do I run a sequence of steps, with different values of n and each step using the state from the last step?
so for example the steps would be as follows
step one yields (16,1) which I then want to use as input for my next step with n = 10 which should yield (6, 2). With the 1 from the first step being added to and the 16 in step one being mod with the new n.
n = 25 gives (16,1) then
n = 10 gives (6,2) then
n = 5 gives (1,3) then
n = 1 gives (0,4)
I am aware that using State here might not be correct; but I am trying to use it as a way to learn.
may aim is to implement this function with the state monad.
greedy :: Double -> Int
greedy owed = snd $ foldl go (pennies,0) [25, 10, 5, 1]
where
pennies = floor . (*100) $ owed
go (remaining,counter) coin = (remaining',counter')
where
remaining' = remaining `mod` coin
counter' = counter + remaining `div` coin
haskell monads state-monad
Are you talking about anything other thanstep 1 >> step 2 >> step 3?
– Cubic
Aug 1 '17 at 10:54
@Cubic, I have amended my question.
– matthias
Aug 1 '17 at 11:21
add a comment |
I have a function
step :: Int -> State Int Int
step n = get >>= x -> put (x `div` n) >> return (x `mod` n)
λ> runState (step 25) 41
(16,1)
How do I run a sequence of steps, with different values of n and each step using the state from the last step?
so for example the steps would be as follows
step one yields (16,1) which I then want to use as input for my next step with n = 10 which should yield (6, 2). With the 1 from the first step being added to and the 16 in step one being mod with the new n.
n = 25 gives (16,1) then
n = 10 gives (6,2) then
n = 5 gives (1,3) then
n = 1 gives (0,4)
I am aware that using State here might not be correct; but I am trying to use it as a way to learn.
may aim is to implement this function with the state monad.
greedy :: Double -> Int
greedy owed = snd $ foldl go (pennies,0) [25, 10, 5, 1]
where
pennies = floor . (*100) $ owed
go (remaining,counter) coin = (remaining',counter')
where
remaining' = remaining `mod` coin
counter' = counter + remaining `div` coin
haskell monads state-monad
I have a function
step :: Int -> State Int Int
step n = get >>= x -> put (x `div` n) >> return (x `mod` n)
λ> runState (step 25) 41
(16,1)
How do I run a sequence of steps, with different values of n and each step using the state from the last step?
so for example the steps would be as follows
step one yields (16,1) which I then want to use as input for my next step with n = 10 which should yield (6, 2). With the 1 from the first step being added to and the 16 in step one being mod with the new n.
n = 25 gives (16,1) then
n = 10 gives (6,2) then
n = 5 gives (1,3) then
n = 1 gives (0,4)
I am aware that using State here might not be correct; but I am trying to use it as a way to learn.
may aim is to implement this function with the state monad.
greedy :: Double -> Int
greedy owed = snd $ foldl go (pennies,0) [25, 10, 5, 1]
where
pennies = floor . (*100) $ owed
go (remaining,counter) coin = (remaining',counter')
where
remaining' = remaining `mod` coin
counter' = counter + remaining `div` coin
haskell monads state-monad
haskell monads state-monad
edited Aug 1 '17 at 15:27
duplode
23.2k44987
23.2k44987
asked Aug 1 '17 at 10:53
matthiasmatthias
615510
615510
Are you talking about anything other thanstep 1 >> step 2 >> step 3?
– Cubic
Aug 1 '17 at 10:54
@Cubic, I have amended my question.
– matthias
Aug 1 '17 at 11:21
add a comment |
Are you talking about anything other thanstep 1 >> step 2 >> step 3?
– Cubic
Aug 1 '17 at 10:54
@Cubic, I have amended my question.
– matthias
Aug 1 '17 at 11:21
Are you talking about anything other than
step 1 >> step 2 >> step 3?– Cubic
Aug 1 '17 at 10:54
Are you talking about anything other than
step 1 >> step 2 >> step 3?– Cubic
Aug 1 '17 at 10:54
@Cubic, I have amended my question.
– matthias
Aug 1 '17 at 11:21
@Cubic, I have amended my question.
– matthias
Aug 1 '17 at 11:21
add a comment |
1 Answer
1
active
oldest
votes
The function,
mapM step [25,10,5,1]
or the more general
traverse step [25,10,5,1]
runs step on each of the list of [25,10,5,1]. Invoking
runState (mapM step [25,10,5,1]) 41
runs the function with the initial state set to 41, returning the list of step outputs and the final state.
([16,1,0,0],0)
If you want to list the states along with the output, just modify step to include them.
step n = get >>= x -> put (x `div` n) >> return ((x `mod` n),(x `div` n))
or, put another way
step n = do
x <- get
let (r,x') = (x `mod` n,x `div` n)
put x'
return (r,x')
The result is, ([(16,1),(1,0),(0,0),(0,0)],0), still not what you are looking for, but closer. I'm afraid I don't understand the particulars of your equation well enough to get what you are looking for, but this should help sort out the State part, allowing you to focus on the math.
To make the above go function:
go n = do
(r,c) <- get
let (r',c') = (r `mod` n, c + (r `div` n))
put (r',c')
return (r',c')
and
runState (mapM go [25,10,5,1]) (41,0)
yields,
([(16,1),(6,2),(1,3),(0,4)],(0,4))
Hope that helps
I can't seem to get it to work.. :(
– matthias
Aug 1 '17 at 11:59
runState (mapM step [25,10,5,1]) 41 ==> ([16,1,0,0],0)this is what I get ...
– matthias
Aug 1 '17 at 12:03
my mistake, answered without testing, sorry. its only returning the outputs, will post fix.
– trevor cook
Aug 1 '17 at 12:06
any chance you can explain how the traverse works in this case. Sorry, I have really hit a wall with State Monad just can't get my head around it. I don't get what is being (or how it is being) passed tostepat each point.
– matthias
Aug 1 '17 at 12:24
How do I get the final value from the first result and use it for the second? I seem to only be able to access the state in this way?
– matthias
Aug 1 '17 at 12:27
|
show 6 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45435845%2fchaining-state-monad%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The function,
mapM step [25,10,5,1]
or the more general
traverse step [25,10,5,1]
runs step on each of the list of [25,10,5,1]. Invoking
runState (mapM step [25,10,5,1]) 41
runs the function with the initial state set to 41, returning the list of step outputs and the final state.
([16,1,0,0],0)
If you want to list the states along with the output, just modify step to include them.
step n = get >>= x -> put (x `div` n) >> return ((x `mod` n),(x `div` n))
or, put another way
step n = do
x <- get
let (r,x') = (x `mod` n,x `div` n)
put x'
return (r,x')
The result is, ([(16,1),(1,0),(0,0),(0,0)],0), still not what you are looking for, but closer. I'm afraid I don't understand the particulars of your equation well enough to get what you are looking for, but this should help sort out the State part, allowing you to focus on the math.
To make the above go function:
go n = do
(r,c) <- get
let (r',c') = (r `mod` n, c + (r `div` n))
put (r',c')
return (r',c')
and
runState (mapM go [25,10,5,1]) (41,0)
yields,
([(16,1),(6,2),(1,3),(0,4)],(0,4))
Hope that helps
I can't seem to get it to work.. :(
– matthias
Aug 1 '17 at 11:59
runState (mapM step [25,10,5,1]) 41 ==> ([16,1,0,0],0)this is what I get ...
– matthias
Aug 1 '17 at 12:03
my mistake, answered without testing, sorry. its only returning the outputs, will post fix.
– trevor cook
Aug 1 '17 at 12:06
any chance you can explain how the traverse works in this case. Sorry, I have really hit a wall with State Monad just can't get my head around it. I don't get what is being (or how it is being) passed tostepat each point.
– matthias
Aug 1 '17 at 12:24
How do I get the final value from the first result and use it for the second? I seem to only be able to access the state in this way?
– matthias
Aug 1 '17 at 12:27
|
show 6 more comments
The function,
mapM step [25,10,5,1]
or the more general
traverse step [25,10,5,1]
runs step on each of the list of [25,10,5,1]. Invoking
runState (mapM step [25,10,5,1]) 41
runs the function with the initial state set to 41, returning the list of step outputs and the final state.
([16,1,0,0],0)
If you want to list the states along with the output, just modify step to include them.
step n = get >>= x -> put (x `div` n) >> return ((x `mod` n),(x `div` n))
or, put another way
step n = do
x <- get
let (r,x') = (x `mod` n,x `div` n)
put x'
return (r,x')
The result is, ([(16,1),(1,0),(0,0),(0,0)],0), still not what you are looking for, but closer. I'm afraid I don't understand the particulars of your equation well enough to get what you are looking for, but this should help sort out the State part, allowing you to focus on the math.
To make the above go function:
go n = do
(r,c) <- get
let (r',c') = (r `mod` n, c + (r `div` n))
put (r',c')
return (r',c')
and
runState (mapM go [25,10,5,1]) (41,0)
yields,
([(16,1),(6,2),(1,3),(0,4)],(0,4))
Hope that helps
I can't seem to get it to work.. :(
– matthias
Aug 1 '17 at 11:59
runState (mapM step [25,10,5,1]) 41 ==> ([16,1,0,0],0)this is what I get ...
– matthias
Aug 1 '17 at 12:03
my mistake, answered without testing, sorry. its only returning the outputs, will post fix.
– trevor cook
Aug 1 '17 at 12:06
any chance you can explain how the traverse works in this case. Sorry, I have really hit a wall with State Monad just can't get my head around it. I don't get what is being (or how it is being) passed tostepat each point.
– matthias
Aug 1 '17 at 12:24
How do I get the final value from the first result and use it for the second? I seem to only be able to access the state in this way?
– matthias
Aug 1 '17 at 12:27
|
show 6 more comments
The function,
mapM step [25,10,5,1]
or the more general
traverse step [25,10,5,1]
runs step on each of the list of [25,10,5,1]. Invoking
runState (mapM step [25,10,5,1]) 41
runs the function with the initial state set to 41, returning the list of step outputs and the final state.
([16,1,0,0],0)
If you want to list the states along with the output, just modify step to include them.
step n = get >>= x -> put (x `div` n) >> return ((x `mod` n),(x `div` n))
or, put another way
step n = do
x <- get
let (r,x') = (x `mod` n,x `div` n)
put x'
return (r,x')
The result is, ([(16,1),(1,0),(0,0),(0,0)],0), still not what you are looking for, but closer. I'm afraid I don't understand the particulars of your equation well enough to get what you are looking for, but this should help sort out the State part, allowing you to focus on the math.
To make the above go function:
go n = do
(r,c) <- get
let (r',c') = (r `mod` n, c + (r `div` n))
put (r',c')
return (r',c')
and
runState (mapM go [25,10,5,1]) (41,0)
yields,
([(16,1),(6,2),(1,3),(0,4)],(0,4))
Hope that helps
The function,
mapM step [25,10,5,1]
or the more general
traverse step [25,10,5,1]
runs step on each of the list of [25,10,5,1]. Invoking
runState (mapM step [25,10,5,1]) 41
runs the function with the initial state set to 41, returning the list of step outputs and the final state.
([16,1,0,0],0)
If you want to list the states along with the output, just modify step to include them.
step n = get >>= x -> put (x `div` n) >> return ((x `mod` n),(x `div` n))
or, put another way
step n = do
x <- get
let (r,x') = (x `mod` n,x `div` n)
put x'
return (r,x')
The result is, ([(16,1),(1,0),(0,0),(0,0)],0), still not what you are looking for, but closer. I'm afraid I don't understand the particulars of your equation well enough to get what you are looking for, but this should help sort out the State part, allowing you to focus on the math.
To make the above go function:
go n = do
(r,c) <- get
let (r',c') = (r `mod` n, c + (r `div` n))
put (r',c')
return (r',c')
and
runState (mapM go [25,10,5,1]) (41,0)
yields,
([(16,1),(6,2),(1,3),(0,4)],(0,4))
Hope that helps
edited Nov 15 '18 at 7:59
dopamane
85611024
85611024
answered Aug 1 '17 at 11:54
trevor cooktrevor cook
1,068418
1,068418
I can't seem to get it to work.. :(
– matthias
Aug 1 '17 at 11:59
runState (mapM step [25,10,5,1]) 41 ==> ([16,1,0,0],0)this is what I get ...
– matthias
Aug 1 '17 at 12:03
my mistake, answered without testing, sorry. its only returning the outputs, will post fix.
– trevor cook
Aug 1 '17 at 12:06
any chance you can explain how the traverse works in this case. Sorry, I have really hit a wall with State Monad just can't get my head around it. I don't get what is being (or how it is being) passed tostepat each point.
– matthias
Aug 1 '17 at 12:24
How do I get the final value from the first result and use it for the second? I seem to only be able to access the state in this way?
– matthias
Aug 1 '17 at 12:27
|
show 6 more comments
I can't seem to get it to work.. :(
– matthias
Aug 1 '17 at 11:59
runState (mapM step [25,10,5,1]) 41 ==> ([16,1,0,0],0)this is what I get ...
– matthias
Aug 1 '17 at 12:03
my mistake, answered without testing, sorry. its only returning the outputs, will post fix.
– trevor cook
Aug 1 '17 at 12:06
any chance you can explain how the traverse works in this case. Sorry, I have really hit a wall with State Monad just can't get my head around it. I don't get what is being (or how it is being) passed tostepat each point.
– matthias
Aug 1 '17 at 12:24
How do I get the final value from the first result and use it for the second? I seem to only be able to access the state in this way?
– matthias
Aug 1 '17 at 12:27
I can't seem to get it to work.. :(
– matthias
Aug 1 '17 at 11:59
I can't seem to get it to work.. :(
– matthias
Aug 1 '17 at 11:59
runState (mapM step [25,10,5,1]) 41 ==> ([16,1,0,0],0) this is what I get ...– matthias
Aug 1 '17 at 12:03
runState (mapM step [25,10,5,1]) 41 ==> ([16,1,0,0],0) this is what I get ...– matthias
Aug 1 '17 at 12:03
my mistake, answered without testing, sorry. its only returning the outputs, will post fix.
– trevor cook
Aug 1 '17 at 12:06
my mistake, answered without testing, sorry. its only returning the outputs, will post fix.
– trevor cook
Aug 1 '17 at 12:06
any chance you can explain how the traverse works in this case. Sorry, I have really hit a wall with State Monad just can't get my head around it. I don't get what is being (or how it is being) passed to
step at each point.– matthias
Aug 1 '17 at 12:24
any chance you can explain how the traverse works in this case. Sorry, I have really hit a wall with State Monad just can't get my head around it. I don't get what is being (or how it is being) passed to
step at each point.– matthias
Aug 1 '17 at 12:24
How do I get the final value from the first result and use it for the second? I seem to only be able to access the state in this way?
– matthias
Aug 1 '17 at 12:27
How do I get the final value from the first result and use it for the second? I seem to only be able to access the state in this way?
– matthias
Aug 1 '17 at 12:27
|
show 6 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f45435845%2fchaining-state-monad%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you talking about anything other than
step 1 >> step 2 >> step 3?– Cubic
Aug 1 '17 at 10:54
@Cubic, I have amended my question.
– matthias
Aug 1 '17 at 11:21