Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  A simple function V2 (Ezequiel Hernan Di Giorgi)
   2. Re:  A simple function V2 (Gary Klindt)
   3. Re:  A simple function V2 (Gary Klindt)
   4. Re:  A simple function V2 (Ertugrul S?ylemez)
   5. Re:  A simple function V2 (Lyndon Maydwell)
   6. Re:  A simple function V2 (Brent Yorgey)
   7. Re:  A simple function V2 (Ertugrul S?ylemez)


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

Message: 1
Date: Fri, 31 Aug 2012 09:01:30 -0300
From: Ezequiel Hernan Di Giorgi <hernan.digio...@gmail.com>
Subject: [Haskell-beginners] A simple function V2
To: beginners@haskell.org
Message-ID:
        <cahrx9sxsnwd-jjh2zy5bdhfrkzr+ed-c4c1tddpi8zbjyju...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

First i want to thank all the persons who responded me yesterday to help
me. Thanks! I am so happy with with your friendliness.
So i have other beginners question:

Now i want a improved version of my* intercalate*. Now i want to call a
function with two [t][t] and obtain another one which have only
even elements, even because:

   - [1,2,3,3,4][6] and the ouput [1,6]
   - [1,2,3,4][5,6,7] output [1,5,2,6,3,7]

I tried it:

*intercalate :: (Eq t) => [t] -> [t] -> [t]*
*intercalate (x:xs) (y:ys)*
* | xt == [] = []*
* | yt == [] = []*
* | otherwise = x : y : intercalate xs ys*
* where xt=(x:xs)*
*            yt=(y:ys)*

but i get nice error

**Main> intercalate [1][6]*
*[1,6*** Exception: baby.hs:(2,1)-(5,51): Non-exhaustive patterns in
function intercalate*
*
*
**Main> *

(yes...the file's name is baby.hs)

Thanks in advance! (: (: (:
(: (:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120831/5b1e2b3b/attachment-0001.htm>

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

Message: 2
Date: Fri, 31 Aug 2012 14:06:02 +0200
From: Gary Klindt <gary.kli...@googlemail.com>
Subject: Re: [Haskell-beginners] A simple function V2
To: Ezequiel Hernan Di Giorgi <hernan.digio...@gmail.com>
Cc: beginners@haskell.org
Message-ID: <5040a8aa.1070...@googlemail.com>
Content-Type: text/plain; charset=UTF-8; format=flowed

Hi,

you don't proof for empty lists, only for empty tails: (x:[])


On 08/31/2012 02:01 PM, Ezequiel Hernan Di Giorgi wrote:
> First i want to thank all the persons who responded me yesterday to help
> me. Thanks! I am so happy with with your friendliness.
> So i have other beginners question:
>
> Now i want a improved version of my* intercalate*. Now i want to call a
> function with two [t][t] and obtain another one which have only
> even elements, even because:
>
>     - [1,2,3,3,4][6] and the ouput [1,6]
>     - [1,2,3,4][5,6,7] output [1,5,2,6,3,7]
>
> I tried it:
>
> *intercalate :: (Eq t) => [t] -> [t] -> [t]*
> *intercalate (x:xs) (y:ys)*
> * | xt == [] = []*
> * | yt == [] = []*
> * | otherwise = x : y : intercalate xs ys*
> * where xt=(x:xs)*
> *            yt=(y:ys)*
>
> but i get nice error
>
> **Main> intercalate [1][6]*
> *[1,6*** Exception: baby.hs:(2,1)-(5,51): Non-exhaustive patterns in
> function intercalate*
> *
> *
> **Main> *
>
> (yes...the file's name is baby.hs)
>
> Thanks in advance! (: (: (:
> (: (:
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



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

Message: 3
Date: Fri, 31 Aug 2012 14:16:06 +0200
From: Gary Klindt <gary.kli...@googlemail.com>
Subject: Re: [Haskell-beginners] A simple function V2
To: Ezequiel Hernan Di Giorgi <hernan.digio...@gmail.com>
Cc: beginners@haskell.org
Message-ID: <5040ab06.8080...@googlemail.com>
Content-Type: text/plain; charset=UTF-8; format=flowed

my mistake, that's not right.

But you should try to use use something like this:

intercalate :: (Eq t) => [t] -> [t] -> [t]
intercalate _ [] = []
intercalate [] _ = []
intercalate (x:xs) (y:ys) = x : y : intercalate xs ys

with the last line, you imply that your lists have at least one element 
(through pattern matching). By the recursive call, you will enter this 
'non-exhaustive' case.

Also, instead of writing

*intercalate (x:xs) (y:ys)*
 >> * | xt == [] = []*
 >> * | yt == [] = []*
 >> * | otherwise = x : y : intercalate xs ys*
* where xt=(x:xs)*
 >> *            yt=(y:ys)*

for defining xt and yt, you could write:

intercalate xt@(x:xs) yt@(y:ys)


greets



On 08/31/2012 02:06 PM, Gary Klindt wrote:
> Hi,
>
> you don't proof for empty lists, only for empty tails: (x:[])
>
>
> On 08/31/2012 02:01 PM, Ezequiel Hernan Di Giorgi wrote:
>> First i want to thank all the persons who responded me yesterday to help
>> me. Thanks! I am so happy with with your friendliness.
>> So i have other beginners question:
>>
>> Now i want a improved version of my* intercalate*. Now i want to call a
>> function with two [t][t] and obtain another one which have only
>> even elements, even because:
>>
>>     - [1,2,3,3,4][6] and the ouput [1,6]
>>     - [1,2,3,4][5,6,7] output [1,5,2,6,3,7]
>>
>> I tried it:
>>
>> *intercalate :: (Eq t) => [t] -> [t] -> [t]*
>> *intercalate (x:xs) (y:ys)*
>> * | xt == [] = []*
>> * | yt == [] = []*
>> * | otherwise = x : y : intercalate xs ys*
>> * where xt=(x:xs)*
>> *            yt=(y:ys)*
>>
>> but i get nice error
>>
>> **Main> intercalate [1][6]*
>> *[1,6*** Exception: baby.hs:(2,1)-(5,51): Non-exhaustive patterns in
>> function intercalate*
>> *
>> *
>> **Main> *
>>
>> (yes...the file's name is baby.hs)
>>
>> Thanks in advance! (: (: (:
>> (: (:
>>
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners@haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>



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

Message: 4
Date: Fri, 31 Aug 2012 14:37:16 +0200
From: Ertugrul S?ylemez <e...@ertes.de>
Subject: Re: [Haskell-beginners] A simple function V2
To: beginners@haskell.org
Message-ID: <20120831143716.4fcc4...@tritium.streitmacht.eu>
Content-Type: text/plain; charset="us-ascii"

Ezequiel Hernan Di Giorgi <hernan.digio...@gmail.com> wrote:

> *intercalate :: (Eq t) => [t] -> [t] -> [t]*
> *intercalate (x:xs) (y:ys)*
> * | xt == [] = []*
> * | yt == [] = []*
> * | otherwise = x : y : intercalate xs ys*
> * where xt=(x:xs)*
> *            yt=(y:ys)*

As a beginner it's almost always wrong to use the comparison operator
(==).  Use it only to compare numbers, for everything else use
pattern-matching.  In particular use guards ("| ... = ...") only when
they are really necessary.  In this case they are not.

Put differently, if your function needs an Eq constraint (or any
constraint) you're doing it wrong.  The type signature should be:

    intercalate :: [a] -> [a] -> [a]


Greets,
Ertugrul

-- 
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120831/66c9e2d9/attachment-0001.pgp>

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

Message: 5
Date: Fri, 31 Aug 2012 20:47:00 +0800
From: Lyndon Maydwell <maydw...@gmail.com>
Subject: Re: [Haskell-beginners] A simple function V2
To: Gary Klindt <gary.kli...@googlemail.com>
Cc: beginners@haskell.org
Message-ID:
        <cam5qztxbxejleuqcukwtcmtqng4fde6nwnab5xoojc9u1nf...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

You can also take advantage of higher order functions to simplify the
code. Something like:

doubleton :: a -> a -> [a]
doubleton x y = [x,y]

pre_intercalate :: [t] -> [t] -> [[t]]
pre_intercalate xs ys = zipWith doubleton xs ys

...

Then you're just one function-call (you can find it on Hoogle when you
determine what the signature needs to be) away from a solution. This
has the advantage of doing away with base cases and recursion. You
should definitely try out this style of whole-structure manipulation
as the Haskell standard libraries make it very convenient.

On Fri, Aug 31, 2012 at 8:16 PM, Gary Klindt <gary.kli...@googlemail.com> wrote:
> my mistake, that's not right.
>
> But you should try to use use something like this:
>
>
> intercalate :: (Eq t) => [t] -> [t] -> [t]
> intercalate _ [] = []
> intercalate [] _ = []
> intercalate (x:xs) (y:ys) = x : y : intercalate xs ys
>
> with the last line, you imply that your lists have at least one element
> (through pattern matching). By the recursive call, you will enter this
> 'non-exhaustive' case.
>
> Also, instead of writing
>
>
> *intercalate (x:xs) (y:ys)*
>>> * | xt == [] = []*
>>> * | yt == [] = []*
>>> * | otherwise = x : y : intercalate xs ys*
> * where xt=(x:xs)*
>>> *            yt=(y:ys)*
>
> for defining xt and yt, you could write:
>
> intercalate xt@(x:xs) yt@(y:ys)
>
>
> greets
>
>
>
>
> On 08/31/2012 02:06 PM, Gary Klindt wrote:
>>
>> Hi,
>>
>> you don't proof for empty lists, only for empty tails: (x:[])
>>
>>
>> On 08/31/2012 02:01 PM, Ezequiel Hernan Di Giorgi wrote:
>>>
>>> First i want to thank all the persons who responded me yesterday to help
>>> me. Thanks! I am so happy with with your friendliness.
>>> So i have other beginners question:
>>>
>>> Now i want a improved version of my* intercalate*. Now i want to call a
>>> function with two [t][t] and obtain another one which have only
>>> even elements, even because:
>>>
>>>     - [1,2,3,3,4][6] and the ouput [1,6]
>>>     - [1,2,3,4][5,6,7] output [1,5,2,6,3,7]
>>>
>>> I tried it:
>>>
>>> *intercalate :: (Eq t) => [t] -> [t] -> [t]*
>>> *intercalate (x:xs) (y:ys)*
>>> * | xt == [] = []*
>>> * | yt == [] = []*
>>> * | otherwise = x : y : intercalate xs ys*
>>> * where xt=(x:xs)*
>>> *            yt=(y:ys)*
>>>
>>> but i get nice error
>>>
>>> **Main> intercalate [1][6]*
>>> *[1,6*** Exception: baby.hs:(2,1)-(5,51): Non-exhaustive patterns in
>>> function intercalate*
>>> *
>>> *
>>> **Main> *
>>>
>>> (yes...the file's name is baby.hs)
>>>
>>> Thanks in advance! (: (: (:
>>> (: (:
>>>
>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners@haskell.org
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



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

Message: 6
Date: Fri, 31 Aug 2012 09:52:40 -0400
From: Brent Yorgey <byor...@seas.upenn.edu>
Subject: Re: [Haskell-beginners] A simple function V2
To: beginners@haskell.org
Message-ID: <20120831135240.ga19...@seas.upenn.edu>
Content-Type: text/plain; charset=us-ascii

On Fri, Aug 31, 2012 at 09:01:30AM -0300, Ezequiel Hernan Di Giorgi wrote:
> 
> *intercalate :: (Eq t) => [t] -> [t] -> [t]*
> *intercalate (x:xs) (y:ys)*
> * | xt == [] = []*
> * | yt == [] = []*
> * | otherwise = x : y : intercalate xs ys*
> * where xt=(x:xs)*
> *            yt=(y:ys)*

I should also point out that the tests xt == [] and yt == [] will
never be true!  That is because xt is defined to be (x:xs) and yt is
(y:ys).  A list can be *either* the empty list [], or a cons like
(x:xs), but not both.

-Brent



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

Message: 7
Date: Fri, 31 Aug 2012 16:15:38 +0200
From: Ertugrul S?ylemez <e...@ertes.de>
Subject: Re: [Haskell-beginners] A simple function V2
To: beginners@haskell.org
Message-ID: <20120831161538.6b33a...@tritium.streitmacht.eu>
Content-Type: text/plain; charset="us-ascii"

Brent Yorgey <byor...@seas.upenn.edu> wrote:

> > *intercalate :: (Eq t) => [t] -> [t] -> [t]*
> > *intercalate (x:xs) (y:ys)*
> > * | xt == [] = []*
> > * | yt == [] = []*
> > * | otherwise = x : y : intercalate xs ys*
> > * where xt=(x:xs)*
> > *            yt=(y:ys)*
>
> I should also point out that the tests xt == [] and yt == [] will
> never be true!  That is because xt is defined to be (x:xs) and yt is
> (y:ys).

That's actually not true.  See his 'where' clause at the bottom.


Greets,
Ertugrul

-- 
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120831/6fa93f8d/attachment.pgp>

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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 50, Issue 40
*****************************************

Reply via email to