Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Converting an imperative program to haskell (Hein Hundal)
   2. Re:  Re: Iterating through a list of char... (matthew coolbeth)
   3. Re:  Re: Iterating through a list of char... (Ozgur Akgun)


----------------------------------------------------------------------

Message: 1
Date: Thu, 29 Apr 2010 13:29:40 -0700 (PDT)
From: Hein Hundal <[email protected]>
Subject: [Haskell-beginners] Converting an imperative program to
        haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Hi,

   I am new to Haskell.  I programmed in imperative languages 
for many years and I have been using Mathematica for the last 
15 years which mixes functional and imperative programming.  
I started learning Haskell about 2 months ago mostly by reading 
the first 13.5 chapters of Real World Haskell and doing about 50 
of the Euler Problems.  "The Evolution of a Haskell Programmer", 
the Haskell Wiki, and this listserv have also been helpful.

   I figured I should try a larger program in Haskell, so I am
converting one of my Mathematica programs, a simulator for the 
card game Dominion, over to Haskell.  Most of that is going well except for one 
patch of imperative code.  My Haskell version of 
this code is ugly.  I was hoping someone could recommend a better 
way to do it.  I will paste a simplified version of the code 
below.  If necessary, I can provide all the other code used for 
this simplified example (about 30 more lines of Haskell code, or 
an equivalent program in C++ (130 lines of code).

Cheers,
Hein

-----------------Ugly Haskell Code-------------------------
data LoopState = LSt GameState Strat Int Int Int [Int] Int Int Bool

proc :: GameState -> Strat -> (Int, Int, GameState)
proc gs' strat = let 
      iA     = 1
      iTh    = 0
      i      = 0 
      aA     = replicate 20 0
      iB     = 1
      iC     = 0
      bDone  = False 
      gs     = apps ("<<<"++show(gs')++">>>") gs'
      lst    = LSt gs strat iA iTh i aA iB iC bDone
      lst2   = until isDone procloop lst  
      isDone  (LSt gs strat iA iTh i aA iB iC bDone) = bDone
      output  (LSt gs strat iA iTh i aA iB iC bDone) = (iB, iC, appd aA gs)
  in output lst2


procloop :: LoopState -> LoopState
procloop (LSt gs' strat iA iTh i aA iB iC bDone) = do
   let iCd = stratchoose strat gs' iA iTh iB aA
   let gs = apps ("iB "++show iB++ "\n" ) gs'
   if iA<=0 || i>=20 || actcd gs <=0 || iCd == -1 
      then LSt gs strat iA iTh (i+1) aA iB iC True
      else if iCd == 1 
         then LSt gs strat iA (iTh+1) (i+1) aA iB iC False
         else let
              gs2 = delh iCd gs
              aA2 = append aA iCd
              (iA3, iC3, iB3, gs3, aA3) = doAct iA  iC  iB  gs2 aA2 strat iCd
              (iA4, iC4, iB4, gs4, aA4) = doAct iA3 iC3 iB3 gs3 aA3 strat iCd
           in if iTh>0 
              then LSt gs4 strat iA4 (iTh-1) (i+1) aA4 iB4 iC4 False
              else LSt gs3 strat iA3 (iTh-1) (i+1) aA3 iB3 iC3 False


---------------end of Haskell code-------------------------------








  








      


------------------------------

Message: 2
Date: Thu, 29 Apr 2010 16:31:14 -0400
From: matthew coolbeth <[email protected]>
Subject: Re: [Haskell-beginners] Re: Iterating through a list of
        char...
To: Ozgur Akgun <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

This works, except for the case where '=' appears more than twice
consecutively.  A string of multiple '=' should be treated as a single '='.

On Thu, Apr 29, 2010 at 16:26, Ozgur Akgun <[email protected]> wrote:

> I won't attempt writing a general-case function now. If I understood your
> (rather long) description correctly, you want to
> - subtract 42 from the ascii value of the char, except when it is preceeded
> by '=', in which case you subtract 106 instead.
>
> foo :: [Char] -> [Char]
> foo ('=':x:xs) = chr (ord x - 106) : foo xs
> foo (x:xs)     = chr (ord x - 42)  : foo xs
> foo _          = []
>
> Hope I understood the problem correctly.
> Best,
>
> On 29 April 2010 20:37, Jean-Nicolas Jolivet 
> <[email protected]>wrote:
>
>> First I would like to thank everyone for the very interesting replies and
>> suggestions I got so far!...
>>
>> I tried to implement (and at the very least understand) most of them!...
>>
>> To add to the context here, what I am trying to do is:
>>
>> -apply a "transformation" to a character (in my case, subtracting 42 to
>> its ASCII value, which I obtain with chr(ord(c) - 42)
>> -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...
>> -if the character is the escape character itself, '=',  then skip it
>> altogether (keeping in mind that the next character needs to be escaped)...
>>
>> 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...
>>
>> 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:  [Just 'f', Just 'o', Just
>> 'o', Nothing]... I am mapping this using mapMaybe to end up with a proper
>> String...
>>
>> 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?
>>
>> Once again, thanks everyone for all the suggestions!
>>
>> Jean-Nicolas Jolivet
>>
>> On 2010-04-28, at 10:56 AM, Jean-Nicolas Jolivet wrote:
>>
>> > Hi there!
>> >
>> > I'm trying to iterate through each character of a string (that part I
>> > can do!) however, I need to apply a transformation to each
>> > character...based on the previous character in the string! This is the
>> > part I have no clue how to do!
>> >
>> > I'm totally new to Haskell so I'm pretty sure I'm missing something
>> > obvious... I tried with list comprehensions...map... etc... but I
>> > can't figure out how I can access the previous character in my string
>> > in each "iteration".... to use simple pseudo code, what i need to do
>> > is:
>> >
>> > while i < my_string length:
>> >       if my_string[i-1] == some_char:
>> >               do something with my_string[i]
>> >       else
>> >               do something else with my_string[i]
>> >
>> > I'm using imperative programming here obviously since it's what I am
>> > familiar with...but any help as to how I could "translate" this to
>> > functional programming would be really appreciated!
>> >
>> >
>> > Jean-Nicolas Jolivet
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
>
> --
> Ozgur Akgun
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>


-- 
mac
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100429/245c97c0/attachment-0001.html

------------------------------

Message: 3
Date: Thu, 29 Apr 2010 21:37:42 +0100
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] Re: Iterating through a list of
        char...
To: matthew coolbeth <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

ok then,

foo ('=':'=':x:xs) = foo ('=':x:xs)
foo ('=':x:xs)     = chr (ord x - 106) : foo xs
foo (x:xs)         = chr (ord x - 42)  : foo xs
foo _              = []

On 29 April 2010 21:31, matthew coolbeth <[email protected]> wrote:

> This works, except for the case where '=' appears more than twice
> consecutively.  A string of multiple '=' should be treated as a single '='.
>
>
> On Thu, Apr 29, 2010 at 16:26, Ozgur Akgun <[email protected]> wrote:
>
>> I won't attempt writing a general-case function now. If I understood your
>> (rather long) description correctly, you want to
>> - subtract 42 from the ascii value of the char, except when it is
>> preceeded by '=', in which case you subtract 106 instead.
>>
>> foo :: [Char] -> [Char]
>> foo ('=':x:xs) = chr (ord x - 106) : foo xs
>> foo (x:xs)     = chr (ord x - 42)  : foo xs
>> foo _          = []
>>
>> Hope I understood the problem correctly.
>> Best,
>>
>> On 29 April 2010 20:37, Jean-Nicolas Jolivet 
>> <[email protected]>wrote:
>>
>>> First I would like to thank everyone for the very interesting replies and
>>> suggestions I got so far!...
>>>
>>> I tried to implement (and at the very least understand) most of them!...
>>>
>>> To add to the context here, what I am trying to do is:
>>>
>>> -apply a "transformation" to a character (in my case, subtracting 42 to
>>> its ASCII value, which I obtain with chr(ord(c) - 42)
>>> -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...
>>> -if the character is the escape character itself, '=',  then skip it
>>> altogether (keeping in mind that the next character needs to be escaped)...
>>>
>>> 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...
>>>
>>> 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:  [Just 'f', Just 'o', Just
>>> 'o', Nothing]... I am mapping this using mapMaybe to end up with a proper
>>> String...
>>>
>>> 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?
>>>
>>> Once again, thanks everyone for all the suggestions!
>>>
>>> Jean-Nicolas Jolivet
>>>
>>> On 2010-04-28, at 10:56 AM, Jean-Nicolas Jolivet wrote:
>>>
>>> > Hi there!
>>> >
>>> > I'm trying to iterate through each character of a string (that part I
>>> > can do!) however, I need to apply a transformation to each
>>> > character...based on the previous character in the string! This is the
>>> > part I have no clue how to do!
>>> >
>>> > I'm totally new to Haskell so I'm pretty sure I'm missing something
>>> > obvious... I tried with list comprehensions...map... etc... but I
>>> > can't figure out how I can access the previous character in my string
>>> > in each "iteration".... to use simple pseudo code, what i need to do
>>> > is:
>>> >
>>> > while i < my_string length:
>>> >       if my_string[i-1] == some_char:
>>> >               do something with my_string[i]
>>> >       else
>>> >               do something else with my_string[i]
>>> >
>>> > I'm using imperative programming here obviously since it's what I am
>>> > familiar with...but any help as to how I could "translate" this to
>>> > functional programming would be really appreciated!
>>> >
>>> >
>>> > Jean-Nicolas Jolivet
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>
>>
>>
>> --
>> Ozgur Akgun
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
>
> --
> mac
>



-- 
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100429/ac87fc86/attachment.html

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 22, Issue 47
*****************************************

Reply via email to