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.  Iterating through a list of char... (Jean-Nicolas Jolivet)
   2.  Re: Iterating through a list of char... (Maciej Piechotka)
   3. Re:  Re: Iterating through a list of char...
      (Jean-Nicolas Jolivet)
   4. Re:  Re: Iterating through a list of char... (Dupont Corentin)
   5. Re:  Iterating through a list of char... (Ozgur Akgun)
   6. Re:  Iterating through a list of char... (Hector Guilarte)


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

Message: 1
Date: Wed, 28 Apr 2010 10:56:37 -0400
From: Jean-Nicolas Jolivet <[email protected]>
Subject: [Haskell-beginners] Iterating through a list of char...
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

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


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

Message: 2
Date: Wed, 28 Apr 2010 16:16:25 +0100
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: Iterating through a list of char...
To: [email protected]
Message-ID: <1272467784.14429.31.ca...@localhost>
Content-Type: text/plain; charset="utf-8"

On Wed, 2010-04-28 at 10:56 -0400, 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:

I assume i > 0 as otherwise my_string[i-1] is illegal

>       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

Very simple version:

func (x:xs) = x:helper x xs
func []     = []

helper p (x:xs) | p == some_char = x':helper x' xs
                | otherwise      = x'':helper x'' xs
                where x' = doSomething x
                      x'' = doSomethingElse x
helper p []                      = []

or (for helper):

helper p (x:xs) = let x' | p == some_char = doSomething x
                         | otherwise      = doSomethingElse x
                  in x':helper x' xs
helper p []     = []

Advanced version (depends on state of character before change)

func s@(x:xs) = x:zipWith proc s xs
                where proc (p, x) | p == some_char = doSomething x
                                  | otherwise      = soSomethingElse x

Advanced version:

func (x:xs) = let s = x:zipWith proc s xs
                  proc (p, x) | p == some_char = doSomething x
                              | otherwise      = soSomethingElse x
              in s

It can be also expressed using folds. Choose version which suits you
best.

Regards
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20100428/17c022a2/attachment-0001.bin

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

Message: 3
Date: Wed, 28 Apr 2010 11:39:52 -0400
From: Jean-Nicolas Jolivet <[email protected]>
Subject: Re: [Haskell-beginners] Re: Iterating through a list of
        char...
To: Maciej Piechotka <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Thanks a lot! I love the fact that you included different versions! Will 
definitely look into each versions and try to understand them all!

Jean-Nicolas


On 2010-04-28, at 11:16 AM, Maciej Piechotka wrote:

> On Wed, 2010-04-28 at 10:56 -0400, 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:
> 
> I assume i > 0 as otherwise my_string[i-1] is illegal
> 
>>      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
> 
> Very simple version:
> 
> func (x:xs) = x:helper x xs
> func []     = []
> 
> helper p (x:xs) | p == some_char = x':helper x' xs
>                | otherwise      = x'':helper x'' xs
>                where x' = doSomething x
>                      x'' = doSomethingElse x
> helper p []                      = []
> 
> or (for helper):
> 
> helper p (x:xs) = let x' | p == some_char = doSomething x
>                         | otherwise      = doSomethingElse x
>                  in x':helper x' xs
> helper p []     = []
> 
> Advanced version (depends on state of character before change)
> 
> func s@(x:xs) = x:zipWith proc s xs
>                where proc (p, x) | p == some_char = doSomething x
>                                  | otherwise      = soSomethingElse x
> 
> Advanced version:
> 
> func (x:xs) = let s = x:zipWith proc s xs
>                  proc (p, x) | p == some_char = doSomething x
>                              | otherwise      = soSomethingElse x
>              in s
> 
> It can be also expressed using folds. Choose version which suits you
> best.
> 
> Regards
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners



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

Message: 4
Date: Wed, 28 Apr 2010 17:40:04 +0200
From: Dupont Corentin <[email protected]>
Subject: Re: [Haskell-beginners] Re: Iterating through a list of
        char...
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hello,

I would do:

doSmthg a = if a == 'a' then (const 'A') else id

trans c = zipWith doSmthg (" " ++ c) c

> trans "arga"
"aAga"

The idea is to zip the string with the same string with an offset of character.
I thing my solution could be cleaner by using Maybe monad to handle
the first character's problem.

Corentin



On 4/28/10, Maciej Piechotka <[email protected]> wrote:
> On Wed, 2010-04-28 at 10:56 -0400, 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:
>
> I assume i > 0 as otherwise my_string[i-1] is illegal
>
>>      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
>
> Very simple version:
>
> func (x:xs) = x:helper x xs
> func []     = []
>
> helper p (x:xs) | p == some_char = x':helper x' xs
>                 | otherwise      = x'':helper x'' xs
>                 where x' = doSomething x
>                       x'' = doSomethingElse x
> helper p []                      = []
>
> or (for helper):
>
> helper p (x:xs) = let x' | p == some_char = doSomething x
>                          | otherwise      = doSomethingElse x
>                   in x':helper x' xs
> helper p []     = []
>
> Advanced version (depends on state of character before change)
>
> func s@(x:xs) = x:zipWith proc s xs
>                 where proc (p, x) | p == some_char = doSomething x
>                                   | otherwise      = soSomethingElse x
>
> Advanced version:
>
> func (x:xs) = let s = x:zipWith proc s xs
>                   proc (p, x) | p == some_char = doSomething x
>                               | otherwise      = soSomethingElse x
>               in s
>
> It can be also expressed using folds. Choose version which suits you
> best.
>
> Regards
>


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

Message: 5
Date: Wed, 28 Apr 2010 16:40:44 +0100
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] Iterating through a list of char...
To: Jean-Nicolas Jolivet <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

Hi!

Since the function to apply depends on the current element, and the previous
element, you need to define what to do if an element doesn't have a
previous, namely the first element in the list. I guess then it'll be quite
easy to implement such a function using explicit recursion.

The function to apply at each step is of type :: a -> a -> a (take the
previous element, and this element, and generate an element to replace the
current element, right?)
The list is of type [a], and the result will be of type [a] again.

foo :: (a -> a -> a) -> [a] -> [a]
foo f (x:y:rest) = f x y : foo f (y:rest)
foo f _ = []

This function is ready-to-use, except a case to handle the first element. It
would simply delete the first element.

*> foo (+) [1,2,3]
[3,5]

You can of course have a function like callfoo, which will prepend the first
element untouched.

callfoo :: (a -> a -> a) -> [a] -> [a]
callfoo f (x:xs) = x : foo f (x:xs)


I would generalise on this a little bit more, and introduce a function to
handle the first element differently.

bar :: (a -> b) -> (a -> a -> b) -> [a] -> [b]
bar f g (x:xs) = f x : loop g (x:xs)
    where loop t (i:j:rest) = t i j : loop t (j:rest)
          loop _ _ = []
bar _ _ _ = []

I think, the best thing about this version is that it doesn't limit the
input and the output of the function to be of the same type! (Figuring out
the meaning of a's and b's is left to the reader)
Note that the loop function defined within bar is almost the same with foo
defined before.

to test:

*> bar id (+) [1,2,3]
[1,3,5]


Hope this will be helpful, and not confusing. If you feel confused, just
think about the types one more time :)

Best,

On 28 April 2010 15:56, Jean-Nicolas Jolivet <[email protected]>
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100428/801aae56/attachment-0001.html

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

Message: 6
Date: Wed, 28 Apr 2010 15:44:14 +0000
From: "Hector Guilarte" <[email protected]>
Subject: Re: [Haskell-beginners] Iterating through a list of char...
To: "Jean-Nicolas Jolivet" <[email protected]>,
        [email protected]
Message-ID:
        
<2030909469-1272469552-cardhu_decombobulator_blackberry.rim.net-21248923...@bda077.bisx.prod.on.blackberry>
        
Content-Type: text/plain; charset="Windows-1252"

Hey!

Keep passing the current char to your next call of the function (your next 
"iteration" as you said), that way you have the previos char in the current 
"iteration" and you can use it. The first time you call it make sure you call 
it with a dummy char that you know it will do what supposed to.

Greetings,

Hector Guilarte

Enviado desde mi dispositivo movil BlackBerry® de Digitel.

-----Original Message-----
From: Jean-Nicolas Jolivet <[email protected]>
Date: Wed, 28 Apr 2010 10:56:37 
To: <[email protected]>
Subject: [Haskell-beginners] Iterating through a list of char...

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

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

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


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

Reply via email to