[Haskell-beginners] Iterating through a list of char...

Ozgur Akgun ozgurakgun at gmail.com
Wed Apr 28 11:40:44 EDT 2010


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 <jeannicolascocoa at gmail.com>
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
> Beginners at haskell.org
> 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.html


More information about the Beginners mailing list