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


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

Message: 1
Date: Mon, 10 Nov 2014 13:52:21 -0500
From: Leza Morais Lutonda <[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=windows-1252; format=flowed

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
>
>
>


-- 
Leza Morais Lutonda, Lemol-C
Electronic and Telecommunication Engineer
Software Development and Architecture Enthusiast
http://lemol.github.io



50 Aniversario de la Cujae. Inaugurada por Fidel el 2 de diciembre de 1964  
http://cujae.edu.cu




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

Message: 2
Date: Mon, 10 Nov 2014 19:59:01 +0100
From: Roelof Wobben <[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=ISO-8859-1; format=flowed

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
>>
>>
>>
>
>



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

Message: 3
Date: Mon, 10 Nov 2014 19:41:10 +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:
        <cab0tuzcsnxfdsu+ay8g6srtbteykocfw6ohbpzejgp4jabk...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Consider what happens if you apply the last function to xs.

On Monday, November 10, 2014, Roelof Wobben <[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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20141110/53a5897f/attachment-0001.html>

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

Message: 4
Date: Mon, 10 Nov 2014 20:47:01 +0100
From: Roelof Wobben <[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"

An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20141110/e43819f8/attachment-0001.html>

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

Message: 5
Date: Mon, 10 Nov 2014 20:47:28 +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=utf-8

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


On Mon, Nov 10, 2014 at 06:28:46PM +0100, Roelof Wobben wrote:
>    oke,
>    I try it with this :
>    acc :: a -> Maybe a -> Maybe a
>    acc a acc  = if null a then Nothing else if (null (tail a)) then (Just
>    acc) else acc  a acc
>    else
>    if the tail a  is empty so there is only 1 item then the output must be
>    acc
>    else
>    must be run again ( there is a list with more then 1 item)
>    but now I see this error message :
>    Couldn't match expected type ?a -> Maybe a -> Maybe a? with actual type
>    ?Maybe a? Relevant bindings include acc :: Maybe a
>    and that is on the else part
>    Roelof
>    Benjamin Edwards schreef op 10-11-2014 18:07:
> 
>    > It is complining but as soon as I want to run it , I see this message
>    :
>    > Non-exhaustive patterns in function acc
> 
>      Yep, Maybe has two constructors, and you are only handling one. You
>      should handle the Nothing case as well.
> 
> _______________________________________________
> Beginners mailing list
> [1][email protected]
> [2]http://www.haskell.org/mailman/listinfo/beginners
> 
> References
> 
>    1. mailto:[email protected]
>    2. http://www.haskell.org/mailman/listinfo/beginners

> _______________________________________________
> 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 9
****************************************

Reply via email to