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:  Are these soloutions all valid and a good use of Haskell
      (Julian Birch)
   2. Re:  Are these soloutions all valid and a good use of Haskell
      (Stefan H?ck)
   3. Re:  Are these soloutions all valid and a good use of Haskell
      (Roelof Wobben)
   4. Re:  Are these soloutions all valid and a good use of Haskell
      (Stefan H?ck)
   5. Re:  Are these soloutions all valid and a good use of Haskell
      (Roelof Wobben)


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

Message: 1
Date: Mon, 10 Nov 2014 19:50:05 +0000
From: Julian Birch <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Are these soloutions all valid and a
        good use of Haskell
Message-ID:
        <CAB0TuzAnjfqo+uz_Lr3g_oCAN-JpMQ=Wd+z=SLj=wmhoeah...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Me misunderstanding actually.  I'd forgotten the foldr.  The answer is that
foldr calls your folding function multiple times, do you don't need to
worry about it.

On Monday, November 10, 2014, Roelof Wobben <[email protected]> wrote:

>  Julian Birch schreef op 10-11-2014 20:41:
>
>
> Consider what happens if you apply the last function to xs.
>
>
> Then you get the last item but fold supposes to run over all items.
>
> Or do I misunderstood the question.
>
> Roelof
>
>
>
>
>  On Monday, November 10, 2014, Roelof Wobben <[email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');>> wrote:
>
>> Of course the compiler is right.
>>
>> Im still struggeling.
>>
>> What I try to do is this
>>
>> 1) if acc is empty then a empty list so the output must be Nothing
>> 2) if the input has only one item then it is the last so the output must
>> be that item.
>> 3) if the input has more then 1item then the last item is not reached so
>> acc must have the value of the next item of the input file
>>
>> And I do not see how I can give acc the value of the next value of the
>> input file maybe head xs
>>
>> Roelof
>>
>>
>> Leza Morais Lutonda schreef op 10-11-2014 19:52:
>>
>>> Hi Roelof,
>>>
>>> On 10/11/14 10:10, Roelof Wobben wrote:
>>>
>>>> No problem .
>>>>
>>>> Im strugelling to make acc work.
>>>>
>>>> I try to say that if the input list has only 1 item the outcome is the
>>>> head of that list.
>>>> But doing
>>>>
>>>> acc = Just (head a)  or doing acc = Just (head acc) gives both that acc
>>>> or a is not in scope
>>>>
>>> The `a` in the type signature `acc :: a -> Maybe a -> Maybe a` is not a
>>> variable name, it is a type.
>>>
>>>>
>>>> also doing acc x = Just (head x) gives a error messages that the types
>>>> are not matching.
>>>>
>>> :t Just (head x)
>>> Maybe a
>>>
>>> :t acc x
>>> Maybe a -> Maybe a
>>>
>>> So, the compiler is right!
>>>
>>>
>>>
>>>> Roelof
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
> --
> Sent from an iPhone, please excuse brevity and typos.
>
>
> _______________________________________________
> Beginners mailing [email protected] 
> <javascript:_e(%7B%7D,'cvml','[email protected]');>http://www.haskell.org/mailman/listinfo/beginners
>
>
>

-- 
Sent from an iPhone, please excuse brevity and typos.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20141110/c54319f5/attachment-0001.html>

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

Message: 2
Date: Mon, 10 Nov 2014 20:58:18 +0100
From: Stefan H?ck <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Are these soloutions all valid and a
        good use of Haskell
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

I used to have a blog (well I still do), but it was about functional
reactive programming in Scala. I'm actually quite a noob in Haskell
myself but with some experince in functional programming in Scala. There
is a Scala library called 'scalaz' which was inspired by all the cool
concepts found in Haskell and Cathegory Theory and I learned lots of
stuff there.

Nowadays, two little children keep me busy. No time for blogs. :-)

On Tue, Nov 11, 2014 at 02:05:52AM +0900, Dontdie YCH wrote:
> Another noob here.
> 
> Thanks for this step by step guide. Your writing style is very attractive.
> 
> Do you have blog about Haskell or any programming subject ?
> 
> Thanks.


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

Message: 3
Date: Mon, 10 Nov 2014 20:59:40 +0100
From: Roelof Wobben <[email protected]>
To: [email protected],  The Haskell-Beginners Mailing List -
        Discussion of primarily beginner-level topics related to Haskell
        <[email protected]>
Subject: Re: [Haskell-beginners] Are these soloutions all valid and a
        good use of Haskell
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed

Stefan H?ck schreef op 10-11-2014 20:47:
> Let's do this step by step. We use the following list: [1,2,3]
>
> foldr takes three arguments:
>
>    foldr :: (a -> b -> b) -> b -> [a] -> b
>
> The last of the three has type [a].
> This means, the fold expects a list as an argument here. The
> lower case letter `a` is the type of the items in the list. In
> our case this is `Int`, but we could pass it other lists with
> other element types.
>
> The second argument of the fold has type `b`, this is the result
> type we'd like to get from the fold. In our case this is `Maybe Int`
> since we want to know from the result, whether the list was empty
> or not. Nothing in case of an empty list, Just x with x being the
> right-most item in the non-empty case.
>
> The first argument of the fold is a higher order function. It
> accepts an argument of type `a` (the element type of the list)
> and of type `b` (the result type of the fold) and should return
> a value of type `b`. In our example, `a` is equal to `Int` and b
> is equal to `Maybe Int`.
>
> Therefore, the following cannot possibly work:
>
>>     acc a acc  = if null a then Nothing else if (null (tail a)) then (Just
> the first argument of acc (the value `a` in your implementation) has
> type `a` (note the distinction between a value and a type, both have
> the same name here, but that doesn't need to be the case. You could
> rename the value to x or foo or whatever). In our case, the type
> `a` is `Int` since we fold over a list of Ints. If we were to fold
> over a list of Strings, type `a` would be equal to `String`. Note
> however, that since `a` can be anything (Int, String etc, depending
> on the element type of your list), you cannot possibly call `null`
> on it. null takes a list as an argument, but `a` is not necessarily a
> list. It's the type of the list's elements, not the whole list.
> You do not pass the whole list to the accumulator. You only pass it
> the list's element in single file.
>
> Now, what happens, when we fold over the list? Since we fold from the
> right, our accumulator is passed two arguments: The last value of the
> list and the initial value of type `b` (in our case `Nothing`).
>
> acc 3 Nothing = ???
>
> What should be the result of this? Clearly you'd like to keep the 3 as
> it is actually the result you are looking for. But you cannot return the
> 3 directly. The return type of our function must be `Maybe Int` and the
> type of `3` is `Int`. We first must wrap the `3` in a `Maybe`. There
> is only one way to do that:
>
>    acc 3 Nothing = Maybe 3
>
> (This is pseudocode an will not compile. The real implementation follows
> below)
>
> Now comes the next item in the list: `2`. Function acc gets called
> again, this time with `2` as its first argument, and the result of
> our fold so far which is `Just 3`
>
>    acc 2 (Just 3) = ???
>
> Clearly we do not want to lose the `3`, it's the result we are looking
> for after all!
>
>    acc 2 (Just 3) = Just 3
>
> Finally, the last item (from the right) in the list comes along:
>
>    acc 1 (Just 3) = Just 3
>
> Again we let it pass and get Just 3 as the result.
>
> This is how foldr works. Every element of the list starting with the
> rightmost is passed as an argument to the accumulator function together
> with the value accumulated so far (for which you must provide an initial
> value).
>
> Now, the implementation. We have seen, that we want to get hold of
> the very first value that comes from the list and ignore everything
> else that comes after it.
>
>    acc :: a -> Maybe a -> Maybe a    # This is the function's type signature
>    acc x Nothing  = Just x           # We wrap the first value ...
>    acc x (Just y) = Just y           # and keep hold of it
>
> When we now use this accumulator in our fold, it gets first called with
> arguments `3` and `Nothing` so the first pattern matches. Variable x
> is assigned the value `3` and the result is `Just 3`.
>
> The function gets called again with the second value `2` and `Just 3`,
> the result from the first call. This time, the first pattern does not
> match (Nothing does not match `Just 3`), but the second does. `x` is
> assigned the value `2`, `y` is assigned the value `3` (the one wrapped
> in the `Just`) and the result is `Just 3`. Same for the last element.
>
> Here is the complete implementation:
>
>    last5 :: [a] -> Maybe a
>    last5 = foldr acc Nothing where
>        acc x Nothing  = Just x
>        acc x (Just y) = Just y
>
> Or better
>
>    last5 :: [a] -> Maybe a
>    last5 = foldr acc Nothing where
>        acc x Nothing  = Just x
>        acc _ j        = j
>
> Or even (this one will be harder to figure out)
>    last5 :: [a] -> Maybe a
>    last5 = foldr acc Nothing where
>        acc x = maybe (Just x) Just
>
> Cheers, Stefan
>
>

I understand the theory but i loose it on the implementation.

Lets take the better variant.

last5 :: [a] -> Maybe a
   last5 = foldr acc Nothing where
       acc x Nothing  = Just x
       acc _ j        = j


Let's say we have [1,2,3]

Do I understand that acc = Nothing. and x = 3 ?
and after  acc x Nothing = Just x  acc get's the value 3

But how does x get's the value 3 or does foldr takes care of that ?

Roelof



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

Message: 4
Date: Mon, 10 Nov 2014 21:15:12 +0100
From: Stefan H?ck <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Are these soloutions all valid and a
        good use of Haskell
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

> 
> I understand the theory but i loose it on the implementation.
> 
> Lets take the better variant.
> 
> last5 :: [a] -> Maybe a
>   last5 = foldr acc Nothing where
>       acc x Nothing  = Just x
>       acc _ j        = j
> 
> 
> Let's say we have [1,2,3]
> 
> Do I understand that acc = Nothing. and x = 3 ?

Not, acc is not Nothing. acc is a function of type (a -> Maybe a ->
Maybe a). Nothing is a value of type Maybe a. The two cannot
possibly be the same.
foldr takes care of feeding the proper arguments to `acc`.

> and after  acc x Nothing = Just x  acc get's the value 3

The result of calling `acc` with arguments 3 and Nothing is `Just 3`.
But that's NOT the value of `acc`. acc is still a function. Note that
there is no such thing as in-place mutation in Haskell, so you
cannot just change the value of `acc`. But the RESULT of calling
  
  acc 3 Nothing

is `Just 3`. This result is taken by foldr and passed again to `acc`
together with the next item in the list. This continues until
the list is exhausted and foldr returns the last result it got from
calling `acc`.

> But how does x get's the value 3 or does foldr takes care of that ?

foldr takes care of all that. foldr calls `acc` with the proper
arguments and does all the bookkeeping internally. There is actually
not much to bookkeep. Here's an implementation of foldr

  foldr :: (a -> b -> b) -> b -> [a] -> b
  foldr _ b []     = b
  foldr f b (a:as) = foldr f (f a b) as

So, no great bookkeeping, just plain recursion.

Stefan


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

Message: 5
Date: Mon, 10 Nov 2014 21:43:38 +0100
From: Roelof Wobben <[email protected]>
To: [email protected],  The Haskell-Beginners Mailing List -
        Discussion of primarily beginner-level topics related to Haskell
        <[email protected]>
Subject: Re: [Haskell-beginners] Are these soloutions all valid and a
        good use of Haskell
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Thanks,

I think my error in thinking is that most of the languages store values 
in a variable,
But it seems that haskell do not use it in a fold

Roelof



Stefan H?ck schreef op 10-11-2014 21:15:
>> I understand the theory but i loose it on the implementation.
>>
>> Lets take the better variant.
>>
>> last5 :: [a] -> Maybe a
>>    last5 = foldr acc Nothing where
>>        acc x Nothing  = Just x
>>        acc _ j        = j
>>
>>
>> Let's say we have [1,2,3]
>>
>> Do I understand that acc = Nothing. and x = 3 ?
> Not, acc is not Nothing. acc is a function of type (a -> Maybe a ->
> Maybe a). Nothing is a value of type Maybe a. The two cannot
> possibly be the same.
> foldr takes care of feeding the proper arguments to `acc`.
>
>> and after  acc x Nothing = Just x  acc get's the value 3
> The result of calling `acc` with arguments 3 and Nothing is `Just 3`.
> But that's NOT the value of `acc`. acc is still a function. Note that
> there is no such thing as in-place mutation in Haskell, so you
> cannot just change the value of `acc`. But the RESULT of calling
>    
>    acc 3 Nothing
>
> is `Just 3`. This result is taken by foldr and passed again to `acc`
> together with the next item in the list. This continues until
> the list is exhausted and foldr returns the last result it got from
> calling `acc`.
>
>> But how does x get's the value 3 or does foldr takes care of that ?
> foldr takes care of all that. foldr calls `acc` with the proper
> arguments and does all the bookkeeping internally. There is actually
> not much to bookkeep. Here's an implementation of foldr
>
>    foldr :: (a -> b -> b) -> b -> [a] -> b
>    foldr _ b []     = b
>    foldr f b (a:as) = foldr f (f a b) as
>
> So, no great bookkeeping, just plain recursion.
>
> Stefan
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>



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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 77, Issue 10
*****************************************

Reply via email to