<div dir="ltr">interact is slick! And I love that there's a word for unlines. Thanks, Frerich!</div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jul 17, 2017 at 11:32 PM, Frerich Raabe <span dir="ltr"><<a href="mailto:raabe@froglogic.com" target="_blank">raabe@froglogic.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 2017-07-18 06:10, Jeffrey Brown wrote:<br>
</span><span class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I wrote a 10-line program[1] for converting from org-mode format to smsn-mode format. Both formats use indentation to indicate hierarchy. In<br>
org, a line at level k (levels are positive integers) starts with k asterisks, followed by a space, followed by the text of the line. In<br>
smsn-mode, a line at level k starts with 4*(k-1) spaces, followed by an asterisks, followed by a space.<br>
<br>
I feel like there ought to be an intermediate step where it converts the data to something other than string -- for instance,<br>
<br>
data IndentedLine = IndentedLine Int String | BadLine<br>
<br>
and then generates the output from that.<br>
</blockquote>
<br></span><span class="">
I think generating an intermediate data structure makes a lot of sense.<br>
<br>
To make your program more 'functional', I'd start by factoring out the IO part as early as often. I.e. consider your program to be a function of type 'String -> String': it consumes a string, and yields a string:<br>
<br>
  reformat :: String -> String<br>
<br>
Now, reformatting the input means splitting it into lines, converting each line, and then merging the lines into a single string again, i.e. we can define 'reformat' as:<br>
<br>
  reformat input = unlines (convertLine (lines input))<br>
<br>
To make this type-check, clearly you need some functions with the types<br>
<br>
  lines :: String -> [String]<br>
  convertLine :: String -> String<br>
  unlines :: [String] -> String<br>
<br>
As it happens, the first and the last function are part of the standard library, so we only need to worry about 'convertLine'. Converting a line means parsing the input line and the serialising the parsed data to the output format, i.e.<br>
<br>
  convertLine line = serialiseToOutput (parseLine line)<br>
<br>
At this point, some sort of data structure to pass from parseLine to serialiseToOutput would be useful. You could certainly go for the 'IndentedLine' type you sketched, i.e. the parseLine function can be declared to be of type<br>
<br>
  parseLine :: String -> IndentedLine<br>
<br>
I'll skip defining this function, but it might be that the 'span' function defined in the Data.List module might be useful here. With that at hand, you only need to define the serialiseToOutput function which (in order to make this program type-check) needs to be of type<br>
<br>
  serialiseToOutput :: IndentedLine -> String<br>
<br>
Again, I'll omit the definition here (but the 'replicate' function would probably be useful).<br>
<br>
At this point, you should have your 'reformat' function fully defined and usable from within 'ghci', i.e. you can nicely test it with some manual input. What's missing is to use it in a real program - you could of course plug it into your existing program calling 'readFile', but as a last idea I'd like to mention the standard 'interact' function which, given a function of type 'String -> String', yields an IO action which reads some input from stdin, applies the given function to it, and then prints the output to stdout. A useful helper for defining UNIX-style filter programs.<br>
<br>
I believe one lesson to take from this is to not think about how the program does something ('count the number of * characters, etc.) but rather think about _what_ the program does - in this case, in a top-down fashion. Also, in Haskell, this type-driven development works quite nicely to yield programs which you can tinker with very early on.<br>
<br>
-- <br>
Frerich Raabe - <a href="mailto:raabe@froglogic.com" target="_blank">raabe@froglogic.com</a><br>
<a href="http://www.froglogic.com" rel="noreferrer" target="_blank">www.froglogic.com</a> - Multi-Platform GUI Testing<br></span>
______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/beginners</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Jeff Brown | Jeffrey Benjamin Brown</div><div dir="ltr"><a href="https://msu.edu/~brown202/" style="font-size:12.8px" target="_blank">Website</a>   |   <a href="https://www.facebook.com/mejeff.younotjeff" style="font-size:12.8px" target="_blank">Facebook</a>   |   <a href="https://www.linkedin.com/in/jeffreybenjaminbrown" style="font-size:12.8px" target="_blank">LinkedIn</a><span style="font-size:12.8px">(spammy, so I often miss messages here)   </span><span style="font-size:12.8px">|</span><span style="font-size:12.8px">   </span><a href="https://github.com/jeffreybenjaminbrown" style="font-size:12.8px" target="_blank">Github</a><span style="font-size:12.8px">   </span></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>
</div>