Home Update lets in a monad an unknown number of times. (Template) Haskell
Reply: 0

Update lets in a monad an unknown number of times. (Template) Haskell

Joris Published in 2018-01-11 22:33:16Z

I want to write code that repeats a few lines in a do block an unknown number of times. Pseudocode example:

main = do
    let x = 0
    repeatUntil (x > 50)
        let y = x
        let x = y + 1
    x -- this should return 51.

Note that it's possible that there are multiple lets that are updated in the repeatUntil body and/or that are being used in the guard.

I sadly enough can't really just make a call to some recursive function to which I pass all of my lets as arguments, since the program described above is generated using metaprogramming and it would be a huge pain to make that work (since I don't really keep track of which lets are in the current scope, so I don't know what I have to pass around).

In short: (how) can I execute a sequence of statements until a certain expression evaluates to false, where the statements use "variables" that are stored as lets and that are updated by assigning a new value to them? It's important that I don't have to pass all the lets that are in scope to some function, because I don't keep track of what's in the scope, so I don't know what to pass to that function either. Scanning the statement sequence for all used lets in order to know the (relevant part of the) scope is not really feasible either I think.

I know that this is a very strange question and that the design of the system is flawed, but I can't easily change that.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.34907 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO