<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Haha, sorry for the (rather long) description ;) The ending was really the important part :)<div><br></div><div>Basically I just wanted to explain the context of what I'm trying to do. Like I said, I already figured out a way to do all that... I'm just wondering if there is a better way to get to the final string result than building a list of Maybe Char and mapping it using mapMaybe...(considering that some characters of the initial string will NOT end up in the resulting string)...&nbsp;</div><div><br></div><div>Every solution I found was iterating to a list of character one character at a time, and outputting a character as a result (whether it is by recursion, mapping, etc..), however, what I was trying to explain in my previous post was that; when I am processing the escape character, this character should not be added to the resulting string... This is where I decided to use the Maybe monad..I'm just wondering if there is a better way...!&nbsp;</div><div><br></div><div><br></div><div>Jean-Nicolas Jolivet</div><div><br></div><div><br></div><div><div><br></div><div><br><div><div>On 2010-04-29, at 4:26 PM, Ozgur Akgun wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">I won't attempt writing a general-case function now. If I understood your (rather long) description correctly, you want to<br>- subtract 42 from the ascii value of the char, except when it is preceeded by '=', in which case you subtract 106 instead.<br>
<br><span style="font-family: courier new,monospace;">foo :: [Char] -&gt; [Char]</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">foo ('=':x:xs) = chr (ord x - 106) : foo xs</span><br style="font-family: courier new,monospace;">
foo (x:xs)&nbsp;&nbsp;&nbsp;&nbsp; = chr (ord x - 42)&nbsp; : foo xs<br>foo _&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = []<br><br style="font-family: arial,helvetica,sans-serif;"><span style="font-family: arial,helvetica,sans-serif;">Hope I understood the problem correctly.</span><br style="font-family: arial,helvetica,sans-serif;">
<span style="font-family: arial,helvetica,sans-serif;">Best,</span><br><br></span><div class="gmail_quote">On 29 April 2010 20:37, Jean-Nicolas Jolivet <span dir="ltr">&lt;<a href="mailto:jeannicolascocoa@gmail.com">jeannicolascocoa@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">First I would like to thank everyone for the very interesting replies and suggestions I got so far!...<br>

<br>
I tried to implement (and at the very least understand) most of them!...<br>
<br>
To add to the context here, what I am trying to do is:<br>
<br>
-apply a "transformation" to a character (in my case, subtracting 42 to its ASCII value, which I obtain with chr(ord(c) - 42)<br>
-if the character is preceded by a specific character (that would be, an escape character, in this case '=') then subtract 106 to its value instead of 42...<br>
-if the character is the escape character itself, '=', &nbsp;then skip it altogether (keeping in mind that the next character needs to be escaped)...<br>
<br>
I managed to do it, however I'm not totally satisfied in the way I did it... the problem was that... as I just explained, in some cases, the character that is being processed has to be "skipped" (and by that I mean, not added to the resulting string). This happens when the processed character IS the escape character...<br>

<br>
What I did was to build a List of Maybe Char.... my function does the proper operation on the character and returns a "Just Char" when the character is processed, or Nothing when it is the escaped character... so basically I would end up with something like: &nbsp;[Just 'f', Just 'o', Just 'o', Nothing]... I am mapping this using mapMaybe to end up with a proper String...<br>

<br>
Would there be any more efficient way of doing this? Considering that the escape character should NOT be added to the resulting string, is there any way I can avoid using the Maybe monad?<br>
<br>
Once again, thanks everyone for all the suggestions!<br>
<font color="#888888"><br>
Jean-Nicolas Jolivet<br>
</font><div><div></div><div class="h5"><br>
On 2010-04-28, at 10:56 AM, Jean-Nicolas Jolivet wrote:<br>
<br>
&gt; Hi there!<br>
&gt;<br>
&gt; I'm trying to iterate through each character of a string (that part I<br>
&gt; can do!) however, I need to apply a transformation to each<br>
&gt; character...based on the previous character in the string! This is the<br>
&gt; part I have no clue how to do!<br>
&gt;<br>
&gt; I'm totally new to Haskell so I'm pretty sure I'm missing something<br>
&gt; obvious... I tried with list comprehensions...map... etc... but I<br>
&gt; can't figure out how I can access the previous character in my string<br>
&gt; in each "iteration".... to use simple pseudo code, what i need to do<br>
&gt; is:<br>
&gt;<br>
&gt; while i &lt; my_string length:<br>
&gt; &nbsp; &nbsp; &nbsp; if my_string[i-1] == some_char:<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do something with my_string[i]<br>
&gt; &nbsp; &nbsp; &nbsp; else<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do something else with my_string[i]<br>
&gt;<br>
&gt; I'm using imperative programming here obviously since it's what I am<br>
&gt; familiar with...but any help as to how I could "translate" this to<br>
&gt; functional programming would be really appreciated!<br>
&gt;<br>
&gt;<br>
&gt; Jean-Nicolas Jolivet<br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Ozgur Akgun<br>
</blockquote></div><br></div></div></body></html>