Hmmm very interesting thinking on this. Perhaps ByteStrings would be a good way to go for efficiency of composition.<div><br></div><div>I'd love to see some profiling of all of this as part of the lesson at some point. (Perhaps with vacuum visualization?)</div>
<div><br></div><div>This thread has tackled 3 major tricky issue areas with Haskell so far:</div><div><br></div><div>1. Lazy IO and seq</div><div>2. Roll-your-own-Monad</div><div>3. Data growth profiling.</div><div><br></div>
<div>It's been a good read anyway, and fun to play with the code.</div><div><br></div><div>Dave</div><div><br></div><div><div class="gmail_quote">On Wed, Aug 19, 2009 at 1:00 PM, Peter Verswyvelen <span dir="ltr"><<a href="mailto:bugfact@gmail.com">bugfact@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Wow, very nice cleanup! That's really a good way for me to learn, thanks.<div><div><div><div><div><div><br></div><div>
Well, my intuition told me that strings and ++ wouldn't work, since what we want is an infinite list of output strings, and using ++ would result in (((s1++s2)++s3)++s4)++s5... which is highly inefficient and I think it would keep the complete output text in memory. Using difference lists results in right associative concatenation of s1++(s2++(s3++(s4++... which is efficient and can be garbage collected nicely. At least that's what I guess. I really would like to get a deeper understanding of all this but that will take lots of time and study, but if I'm lucky I still have 20 to 40 years to go, so I won't be bored :-)</div>
<div><div></div><div class="h5">
<div><br></div><div><br></div><div><br></div><div><br></div><div><div class="gmail_quote">On Wed, Aug 19, 2009 at 9:46 PM, David Leimbach <span dir="ltr"><<a href="mailto:leimy2k@gmail.com" target="_blank">leimy2k@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Very cool!<div><br></div><div>I am still wondering what the significance of the DList is with this though, or why it was needed to begin with.</div>
<div><br></div><div>Dave<div><div></div><div><br><br><div class="gmail_quote">On Wed, Aug 19, 2009 at 12:28 PM, Ryan Ingram <span dir="ltr"><<a href="mailto:ryani.spam@gmail.com" target="_blank">ryani.spam@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Added a new version (tested, works with infinite loops, no early output, etc.)<br>
<br>
<a href="http://hpaste.org/fastcgi/hpaste.fcgi/view?id=8316#a8343" target="_blank">http://hpaste.org/fastcgi/hpaste.fcgi/view?id=8316#a8343</a><br>
<br>
I'll put up a short write-up after lunch.<br>
<font color="#888888"><br>
-- ryan<br>
</font><div><div></div><div><br>
On Wed, Aug 19, 2009 at 11:28 AM, Peter Verswyvelen<<a href="mailto:bugfact@gmail.com" target="_blank">bugfact@gmail.com</a>> wrote:<br>
> The cleaned up code didn't seem to work for me, it printed everything before<br>
> asking input again.<br>
> But I added a patch that looks like it supports looping, but I don't<br>
> understand exactly what is going on :-)<br>
> I added the "delay" function which makes appending to the output less<br>
> strict.<br>
> Note that in this version I add a delay to each right argument of >>=, but<br>
> one could also do it manually<br>
> On Wed, Aug 19, 2009 at 7:37 PM, David Leimbach <<a href="mailto:leimy2k@gmail.com" target="_blank">leimy2k@gmail.com</a>> wrote:<br>
>><br>
>> I've corrected it. It still doesn't suffer looping. :-)<br>
>><br>
>> On Wed, Aug 19, 2009 at 10:31 AM, David Leimbach <<a href="mailto:leimy2k@gmail.com" target="_blank">leimy2k@gmail.com</a>><br>
>> wrote:<br>
>>><br>
>>> Doesn't seem to compile.<br>
>>> I nearly never use case statements in my code, so I'm not really sure<br>
>>> what's going on.<br>
>>> neat2.hs:14:39: parse error on input `='<br>
>>> Dave<br>
>>> On Wed, Aug 19, 2009 at 10:23 AM, Ryan Ingram <<a href="mailto:ryani.spam@gmail.com" target="_blank">ryani.spam@gmail.com</a>><br>
>>> wrote:<br>
>>>><br>
>>>> I posted a reply to your paste with a stricter version of S and some<br>
>>>> cleanup.<br>
>>>><br>
>>>> Untested, though I believe it should work without "seq".<br>
>>>><br>
>>>> "case" provides all the strictness you need, I think!<br>
>>>><br>
>>>> -- ryan<br>
>>>><br>
>>>> On Wed, Aug 19, 2009 at 9:28 AM, Peter Verswyvelen<<a href="mailto:bugfact@gmail.com" target="_blank">bugfact@gmail.com</a>><br>
>>>> wrote:<br>
>>>> > Expect more bugs with this though :-) Just found out that looping does<br>
>>>> > not<br>
>>>> > work, it hangs, e.g.<br>
>>>> ><br>
>>>> > test = do<br>
>>>> > out "Enter your first name:"<br>
>>>> > fstName <- inp<br>
>>>> > out "Enter your second name:"<br>
>>>> > sndName <- inp<br>
>>>> > out ("Welcome "++fstName++" "++sndName)<br>
>>>> > out "Goodbye!"<br>
>>>> > test<br>
>>>> ><br>
>>>> > Doesn't seem to work :-) Back to the drawing board.<br>
>>>> ><br>
>>>> > On Wed, Aug 19, 2009 at 5:55 PM, Peter Verswyvelen <<a href="mailto:bugfact@gmail.com" target="_blank">bugfact@gmail.com</a>><br>
>>>> > wrote:<br>
>>>> >><br>
>>>> >> Not at all, use it for whatever you want to :-)<br>
>>>> >> I'm writing this code because I'm preparing to write a bunch of<br>
>>>> >> tutorials<br>
>>>> >> on FRP, and I first wanted to start with simple console based FRP,<br>
>>>> >> e.g.<br>
>>>> >> making a little text adventure game, where the input/choices of the<br>
>>>> >> user<br>
>>>> >> might be parsed ala parsec, using monadic style, applicative style,<br>
>>>> >> and<br>
>>>> >> arrows, and then doing the same with FRP frameworks like Yampa,<br>
>>>> >> Elera,<br>
>>>> >> Reactive, etc...<br>
>>>> >> After that I would start writing tutorials that use OpenGL, making<br>
>>>> >> some<br>
>>>> >> very simple games, again with the above approaches, and ending with a<br>
>>>> >> conversion of a very old game of mine (Zarathrusta written in<br>
>>>> >> assembler from<br>
>>>> >> 1991, which was based on Thrust from 1986, converted by myself in C++<br>
>>>> >> to<br>
>>>> >> PocketPC as G-Pod, and so I would like to make a version in Haskell<br>
>>>> >> that<br>
>>>> >> runs on the iPhone :-)<br>
>>>> >> This of course is a lot of work, and I would like to put this on the<br>
>>>> >> Haskell wiki or a blog or something, so others can contribute and<br>
>>>> >> comment. I<br>
>>>> >> would like to show real examples that explain the shortcomings of the<br>
>>>> >> FRP<br>
>>>> >> approaches, because now this is still a bit blurry to me.<br>
>>>> >><br>
>>>> >> On Wed, Aug 19, 2009 at 5:43 PM, David Leimbach <<a href="mailto:leimy2k@gmail.com" target="_blank">leimy2k@gmail.com</a>><br>
>>>> >> wrote:<br>
>>>> >>><br>
>>>> >>> This Monad you've created is quite excellent. I was trying to do<br>
>>>> >>> something like this about a year ago, to make the input and output<br>
>>>> >>> handling<br>
>>>> >>> of an interactive bowling score card work nicely. I kept running<br>
>>>> >>> into<br>
>>>> >>> issues, and did not believe that seq was going to do the trick.<br>
>>>> >>> Nice work!<br>
>>>> >>> This is a very useful monad I think, it could be called "Prompter"<br>
>>>> >>> or<br>
>>>> >>> something to that effect.<br>
>>>> >>> Do you mind if I use it in some of my code?<br>
>>>> >>> Dave<br>
>>>> >>><br>
>>>> >>> On Wed, Aug 19, 2009 at 8:42 AM, Peter Verswyvelen<br>
>>>> >>> <<a href="mailto:bugfact@gmail.com" target="_blank">bugfact@gmail.com</a>><br>
>>>> >>> wrote:<br>
>>>> >>>><br>
>>>> >>>> LOL. Maybe we should have that coffee together ;-) at least<br>
>>>> >>>> virtually!<br>
>>>> >>>> On Wed, Aug 19, 2009 at 5:39 PM, David Leimbach <<a href="mailto:leimy2k@gmail.com" target="_blank">leimy2k@gmail.com</a>><br>
>>>> >>>> wrote:<br>
>>>> >>>>><br>
>>>> >>>>> Argh... I too have been up too late :-). I edited THE WRONG FILE!<br>
>>>> >>>>> No<br>
>>>> >>>>> wonder your change didn't take effect! :-/<br>
>>>> >>>>> Time for coffee I suppose.<br>
>>>> >>>>><br>
>>>> >>>>> On Wed, Aug 19, 2009 at 8:38 AM, David Leimbach<br>
>>>> >>>>> <<a href="mailto:leimy2k@gmail.com" target="_blank">leimy2k@gmail.com</a>><br>
>>>> >>>>> wrote:<br>
>>>> >>>>>><br>
>>>> >>>>>> This doesn't seem to be working for me interactively though on a<br>
>>>> >>>>>> Mac.<br>
>>>> >>>>>> I still get "Welcome" before I've entered text.<br>
>>>> >>>>>><br>
>>>> >>>>>> On Wed, Aug 19, 2009 at 8:25 AM, Peter Verswyvelen<br>
>>>> >>>>>> <<a href="mailto:bugfact@gmail.com" target="_blank">bugfact@gmail.com</a>><br>
>>>> >>>>>> wrote:<br>
>>>> >>>>>>><br>
>>>> >>>>>>> I fixed it myself but it's really tricky :-)<br>
>>>> >>>>>>> <a href="http://hpaste.org/fastcgi/hpaste.fcgi/view?id=8316#a8330" target="_blank">http://hpaste.org/fastcgi/hpaste.fcgi/view?id=8316#a8330</a><br>
>>>> >>>>>>> The idea is, that when the input is requested, the output that<br>
>>>> >>>>>>> is<br>
>>>> >>>>>>> then generated must be in sync with the input.<br>
>>>> >>>>>>><br>
>>>> >>>>>>> inp = S $ \s i -> let r = (s `D.append` (i `seq` D.empty), head<br>
>>>> >>>>>>> i) in<br>
>>>> >>>>>>> (tail i, r)<br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>> I first had<br>
>>>> >>>>>>><br>
>>>> >>>>>>> inp = S $ \s i -> let r = (i `seq` s, head i) in (tail i, r)<br>
>>>> >>>>>>><br>
>>>> >>>>>>> But that was too eager, since i syncs the input not with the<br>
>>>> >>>>>>> output,<br>
>>>> >>>>>>> but with the function that will generate the output.<br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>> Okay, now I can sleep again :-)<br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>>> On Wed, Aug 19, 2009 at 5:12 PM, Peter Verswyvelen<br>
>>>> >>>>>>> <<a href="mailto:bugfact@gmail.com" target="_blank">bugfact@gmail.com</a>> wrote:<br>
>>>> >>>>>>>><br>
>>>> >>>>>>>> Thanks, but that doesn't really matter in my example, my code<br>
>>>> >>>>>>>> is<br>
>>>> >>>>>>>> just buggy, and I'm not sure why. For example if I change my<br>
>>>> >>>>>>>> test function<br>
>>>> >>>>>>>> so that it outputs lines only, then it still prints Welcome<br>
>>>> >>>>>>>> first before<br>
>>>> >>>>>>>> asking for input.<br>
>>>> >>>>>>>> See<br>
>>>> >>>>>>>> e.g. <a href="http://hpaste.org/fastcgi/hpaste.fcgi/view?id=8316#a8328" target="_blank">http://hpaste.org/fastcgi/hpaste.fcgi/view?id=8316#a8328</a><br>
>>>> >>>>>>>> On Wed, Aug 19, 2009 at 5:00 PM, David Leimbach<br>
>>>> >>>>>>>> <<a href="mailto:leimy2k@gmail.com" target="_blank">leimy2k@gmail.com</a>><br>
>>>> >>>>>>>> wrote:<br>
>>>> >>>>>>>>><br>
>>>> >>>>>>>>> Try LineBuffering.<br>
>>>> >>>>>>>>> I do linewise stuff with interact a lot. You'll find stuff<br>
>>>> >>>>>>>>> like<br>
>>>> >>>>>>>>> unlines . lines<br>
>>>> >>>>>>>>> may help too. In fact I just wrote a blog post about this.<br>
>>>> >>>>>>>>> <a href="http://leimy9.blogspot.com" target="_blank">http://leimy9.blogspot.com</a><br>
>>>> >>>>>>>>> I'm trying to write some interactive code to automate working<br>
>>>> >>>>>>>>> with<br>
>>>> >>>>>>>>> serial console controlled power strips, so I need to either<br>
>>>> >>>>>>>>> use Expect<br>
>>>> >>>>>>>>> (yuck) or do my own thing.<br>
>>>> >>>>>>>>> Dave<br>
>>>> >>>>>>>>><br>
>>>> >>>>>>>>> On Wed, Aug 19, 2009 at 7:35 AM, Peter Verswyvelen<br>
>>>> >>>>>>>>> <<a href="mailto:bugfact@gmail.com" target="_blank">bugfact@gmail.com</a>> wrote:<br>
>>>> >>>>>>>>>><br>
>>>> >>>>>>>>>> Apparently this particular example happens to work on Mac and<br>
>>>> >>>>>>>>>> Linux because of different buffering (thanks Martijn for the<br>
>>>> >>>>>>>>>> help!)<br>
>>>> >>>>>>>>>> To make sure we have no buffering at all, the main function<br>
>>>> >>>>>>>>>> should<br>
>>>> >>>>>>>>>> be:<br>
>>>> >>>>>>>>>><br>
>>>> >>>>>>>>>> main = do<br>
>>>> >>>>>>>>>> hSetBuffering stdout NoBuffering<br>
>>>> >>>>>>>>>> hSetBuffering stdin NoBuffering<br>
>>>> >>>>>>>>>> test<br>
>>>> >>>>>>>>>><br>
>>>> >>>>>>>>>> Now I think it should also be incorrect on Unix systems.<br>
>>>> >>>>>>>>>> I guess the way I'm concatenating the strings is not correct,<br>
>>>> >>>>>>>>>> not<br>
>>>> >>>>>>>>>> sure.<br>
>>>> >>>>>>>>>> I would like to use a graphical tool to show the graph<br>
>>>> >>>>>>>>>> reduction<br>
>>>> >>>>>>>>>> step by step, to get a better understanding of the laziness &<br>
>>>> >>>>>>>>>> strictness.<br>
>>>> >>>>>>>>>> Does such a tool exist? I know people often say this is not<br>
>>>> >>>>>>>>>> usable because<br>
>>>> >>>>>>>>>> the amount of information is too much, but I used to be an<br>
>>>> >>>>>>>>>> assembly language<br>
>>>> >>>>>>>>>> programmer so I still would like to give it a try :-)<br>
>>>> >>>>>>>>>><br>
>>>> >>>>>>>>>><br>
>>>> >>>>>>>>>> On Wed, Aug 19, 2009 at 1:07 PM, Peter Verswyvelen<br>
>>>> >>>>>>>>>> <<a href="mailto:bugfact@gmail.com" target="_blank">bugfact@gmail.com</a>> wrote:<br>
>>>> >>>>>>>>>>><br>
>>>> >>>>>>>>>>> In an attempt to get a deeper understanding of several<br>
>>>> >>>>>>>>>>> monads<br>
>>>> >>>>>>>>>>> (State, ST, IO, ...) I skimmed over some of the research<br>
>>>> >>>>>>>>>>> papers (but didn't<br>
>>>> >>>>>>>>>>> understand all of it, I lack the required education) and<br>
>>>> >>>>>>>>>>> decided to write a<br>
>>>> >>>>>>>>>>> little program myself without using any prefab monad<br>
>>>> >>>>>>>>>>> instances that should<br>
>>>> >>>>>>>>>>> mimic the following:<br>
>>>> >>>>>>>>>>> main = do<br>
>>>> >>>>>>>>>>> putStrLn "Enter your name:"<br>
>>>> >>>>>>>>>>> x <- getLine<br>
>>>> >>>>>>>>>>> putStr "Welcome "<br>
>>>> >>>>>>>>>>> putStrLn x<br>
>>>> >>>>>>>>>>> putStrLn "Goodbye!"<br>
>>>> >>>>>>>>>>> But instead of using IO, I wanted to make my own pure monad<br>
>>>> >>>>>>>>>>> that<br>
>>>> >>>>>>>>>>> gets evaluated with interact, and does the same.<br>
>>>> >>>>>>>>>>> However, I get the following output:<br>
>>>> >>>>>>>>>>> Enter your name:<br>
>>>> >>>>>>>>>>> Welcome ......<br>
>>>> >>>>>>>>>>> So the Welcome is printed too soon.<br>
>>>> >>>>>>>>>>> This is obvious since my monad is lazy, so I tried to put a<br>
>>>> >>>>>>>>>>> seq<br>
>>>> >>>>>>>>>>> at some strategic places to get the same behavior as IO. But<br>
>>>> >>>>>>>>>>> I completely<br>
>>>> >>>>>>>>>>> failed doing so, either the program doesn't print anything<br>
>>>> >>>>>>>>>>> and asks input<br>
>>>> >>>>>>>>>>> first, or it still prints too much output.<br>
>>>> >>>>>>>>>>> Of course I could just use ST, State, transformers, etc, but<br>
>>>> >>>>>>>>>>> this<br>
>>>> >>>>>>>>>>> is purely an exercise I'm doing.<br>
>>>> >>>>>>>>>>> So, I could re-read all papers and look in detail at all the<br>
>>>> >>>>>>>>>>> code, but maybe someone could help me out where to put the<br>
>>>> >>>>>>>>>>> seq or what to do<br>
>>>> >>>>>>>>>>> :-)<br>
>>>> >>>>>>>>>>> The code is<br>
>>>> >>>>>>>>>>> at <a href="http://hpaste.org/fastcgi/hpaste.fcgi/view?id=8316" target="_blank">http://hpaste.org/fastcgi/hpaste.fcgi/view?id=8316</a><br>
>>>> >>>>>>>>>>> Oh btw, the usage of DList here might not be needed;<br>
>>>> >>>>>>>>>>> intuitively<br>
>>>> >>>>>>>>>>> it felt like the correct thing to do, but when it comes to<br>
>>>> >>>>>>>>>>> Haskell, my<br>
>>>> >>>>>>>>>>> intuition is usually wrong ;-)<br>
>>>> >>>>>>>>>>> Thanks a lot,<br>
>>>> >>>>>>>>>>> Peter Verswyvelen<br>
>>>> >>>>>>>>>><br>
>>>> >>>>>>>>>><br>
>>>> >>>>>>>>>> _______________________________________________<br>
>>>> >>>>>>>>>> Haskell-Cafe mailing list<br>
>>>> >>>>>>>>>> <a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
>>>> >>>>>>>>>> <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
>>>> >>>>>>>>>><br>
>>>> >>>>>>>>><br>
>>>> >>>>>>>><br>
>>>> >>>>>>><br>
>>>> >>>>>><br>
>>>> >>>>><br>
>>>> >>>><br>
>>>> >>><br>
>>>> >><br>
>>>> ><br>
>>>> ><br>
>>>> > _______________________________________________<br>
>>>> > Haskell-Cafe mailing list<br>
>>>> > <a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
>>>> > <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
>>>> ><br>
>>>> ><br>
>>><br>
>><br>
><br>
><br>
</div></div></blockquote></div><br></div></div></div>
</blockquote></div><br></div></div></div></div></div></div></div></div>
</blockquote></div><br></div>