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. Re:  Iterating through a list of char... (Jonas Almstr?m Dureg?rd)
   2. Re:  Iterating through a list of char... (Dupont Corentin)
   3. Re:  Iterating through a list of char... (Ozgur Akgun)
   4. Re:  Iterating through a list of char... (Jonas Almstr?m Dureg?rd)


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

Message: 1
Date: Wed, 28 Apr 2010 17:51:56 +0200
From: Jonas Almstr?m Dureg?rd <[email protected]>
Subject: Re: [Haskell-beginners] Iterating through a list of char...
To: Ozgur Akgun <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Ozgur Akgun wrote:
> *> bar id (+) [1,2,3]
> [1,3,5]

Incidentally, scanl1 (+) [1,2,3] == [1,3,5], i.e. scanl1 = bar id.

/Jonas

On 28 April 2010 17:40, Ozgur Akgun <[email protected]> wrote:
> 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
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>


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

Message: 2
Date: Wed, 28 Apr 2010 17:58:47 +0200
From: Dupont Corentin <[email protected]>
Subject: Re: [Haskell-beginners] Iterating through a list of char...
To: Jonas Almstr?m Dureg?rd <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hello,
i have scanl1 (+) [1,2,3] == [1,3,6]
since scanl apply successive reducing.
am i missing something?

On 4/28/10, Jonas Almström Duregård <[email protected]> wrote:
> Ozgur Akgun wrote:
>> *> bar id (+) [1,2,3]
>> [1,3,5]
>
> Incidentally, scanl1 (+) [1,2,3] == [1,3,5], i.e. scanl1 = bar id.
>
> /Jonas
>
> On 28 April 2010 17:40, Ozgur Akgun <[email protected]> wrote:
>> 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
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>


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

Message: 3
Date: Wed, 28 Apr 2010 17:02:47 +0100
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] Iterating through a list of char...
To: Dupont Corentin <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

Exactly:

*> bar id (+) [1..5]
[1,3,5,7,9]
*> scanl1 (+) [1..5]
[1,3,6,10,15]

But they are close enough, I guess :)

2010/4/28 Dupont Corentin <[email protected]>

> Hello,
> i have scanl1 (+) [1,2,3] == [1,3,6]
> since scanl apply successive reducing.
> am i missing something?
>
> On 4/28/10, Jonas Almström DuregÃ¥rd <[email protected]> wrote:
> > Ozgur Akgun wrote:
> >> *> bar id (+) [1,2,3]
> >> [1,3,5]
> >
> > Incidentally, scanl1 (+) [1,2,3] == [1,3,5], i.e. scanl1 = bar id.
> >
> > /Jonas
> >
> > On 28 April 2010 17:40, Ozgur Akgun <[email protected]> wrote:
> >> 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
> >>
> >> _______________________________________________
> >> Beginners mailing list
> >> [email protected]
> >> http://www.haskell.org/mailman/listinfo/beginners
> >>
> >>
> > _______________________________________________
> > 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/171b87fa/attachment-0001.html

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

Message: 4
Date: Wed, 28 Apr 2010 18:06:56 +0200
From: Jonas Almstr?m Dureg?rd <[email protected]>
Subject: Re: [Haskell-beginners] Iterating through a list of char...
To: Dupont Corentin <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

5 or 6, does it really matter? ;)

Sorry about the confusion...

/J

2010/4/28 Dupont Corentin <[email protected]>:
> Hello,
> i have scanl1 (+) [1,2,3] == [1,3,6]
> since scanl apply successive reducing.
> am i missing something?
>
> On 4/28/10, Jonas Almström Duregård <[email protected]> wrote:
>> Ozgur Akgun wrote:
>>> *> bar id (+) [1,2,3]
>>> [1,3,5]
>>
>> Incidentally, scanl1 (+) [1,2,3] == [1,3,5], i.e. scanl1 = bar id.
>>
>> /Jonas
>>
>> On 28 April 2010 17:40, Ozgur Akgun <[email protected]> wrote:
>>> 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
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>>
>> _______________________________________________
>> 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 43
*****************************************

Reply via email to