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:  safetail problem (Christian Maeder)
   2. Re:  safetail problem (Roelof Wobben)
   3. Re:  safetail problem (Christian Maeder)
   4. Re:  safetail problem (Tom Murphy)
   5. Re:  safetail problem (Roelof Wobben)


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

Message: 1
Date: Wed, 13 Jul 2011 18:28:40 +0200
From: Christian Maeder <[email protected]>
Subject: Re: [Haskell-beginners] safetail problem
To: Kyle Murphy <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed

Am 13.07.2011 17:40, schrieb Kyle Murphy:
> Your new code declares that you're expecting a [x,xs] which isn't a
> valid type. What you're expecting is a [x]. Furthermore you then try to
> pattern match against (x,xs) which is a tuple even though your function
> just declared it's expecting a list. What you want is:
>
> safetail :: [x] -> [x]
> safetail (x:xs) = if null[xs] then [] else xs

Here I miss an equation:

   safetail [] = ...

Also "if null[xs] then [] else xs" happens to correctly simplify to "xs" 
because "null[xs]" is never True, because "[xs]" is a singleton list (of 
lists).

C.

>
> (x:xs) is a pattern match using one of the two list constructors (:)
> which has type:
>
> (:) :: a -> [a] -> [a]
>
> This means that (:) takes two arguments, anything, and a list of
> anything, and returns a new list of anything (in this case with the
> first argument pre-pended to the second argument).
>
> The other list constructor ([]) is a zero argument constructor of type:
>
> [] :: [a]
>
> That is, it can be used to construct an empty list of any type.
>
> With these two constructors it's possible to understand how lists
> working in Haskell.
> [1,2,3,4] is syntactic sugar (that is, it's replaced by the compiler for
> you and exists solely for convenience of writing) for 1:2:3:4:[].
> Following the types you have:
> [] -> Constructs an empty list of type [a], we don't know what "a" is yet.
> 4:[] -> Takes an Int (4) and a list [Int] ([]) and constructs a new list
> of type [Int]. The previous type of [a] is forced to be [Int] at this point.
> 3:[4] -> Takes an Int (3) and a list ([4]) and returns a new list ([3,4]).
> 2:[3,4] -> Takes an Int (2) and a list ([3,4]) and returns a new list
> ([2,3,4]).
> 1:[2,3,4] -> Takes an Int (1) and a list ([2,3,4]) and returns a new
> list ([1,2,3,4]).
>
> N.B. I'm not an expert on Haskell, so the following might be wrong in
> some of the details, but is the way I understand lists to function in
> Haskell.
>
> Don't be confused at this point with the empty list constructor [], and
> the list type ([]), which even though they use the same name are
> actually two different things.
> To be clear, the data declaration of List actually looks similar to the
> following:
>
> data ([]) a = [] | (:) a (([]) a)
>
> which could also be written as:
>
> data [a] = [] | a:[a]
>
> The (([]) a) on the left of the equal sign is the Type. The [] on the
> right of the equal sign is the constructor.
>
> -R. Kyle Murphy
> --
> Curiosity was framed, Ignorance killed the cat.
>
>
> On Wed, Jul 13, 2011 at 10:58, Roelof Wobben <[email protected]
> <mailto:[email protected]>> wrote:
>
>
>     With GHCI I get this message :
>
>
>
>     safetail.hs:1:16: parse error on input ','
>
>
>
>     Roelof
>
>
>
>     ----------------------------------------
>      > From: [email protected] <mailto:[email protected]>
>      > To: [email protected] <mailto:[email protected]>
>      > Date: Wed, 13 Jul 2011 14:54:01 +0000
>      > Subject: Re: [Haskell-beginners] safetail problem
>      >
>      >
>      > Hello,
>      >
>      >
>      >
>      > I have GHC installed so I will try this one with ghci.
>      >
>      >
>      >
>      > Roelof
>      >
>      >
>      >
>      > ----------------------------------------
>      > > Subject: Re: [Haskell-beginners] safetail problem
>      > > From: [email protected] <mailto:[email protected]>
>      > > Date: Wed, 13 Jul 2011 08:10:38 -0400
>      > > CC: [email protected] <mailto:[email protected]>
>      > > To: [email protected] <mailto:[email protected]>
>      > >
>      > > On Jul 13, 2011, at 3:07 AM, Roelof Wobben wrote:
>      > >
>      > > > changed it to this : http://codepad.org/ROV4ASAB
>      > >
>      > >
>      > > Hi, Roelof.
>      > >
>      > > Have you installed GHC? You will find the error messages to be
>     more helpful when you use ghci to execute your code.
>      > >
>      > > I don't know Programming in Haskell by Graham Hutton. I am sure
>     that it very good, but maybe not the best for you. You might also
>     try this tutorial.
>      > >
>      > > > http://learnyouahaskell.com/chapters
>      > >
>      > > ____________________
>      > > David Place
>      > > Owner, Panpipes Ho! LLC
>      > > http://panpipesho.com
>      > > [email protected] <mailto:[email protected]>
>      > >
>      > >
>      > >
>      > _______________________________________________
>      > Beginners mailing list
>      > [email protected] <mailto:[email protected]>
>      > http://www.haskell.org/mailman/listinfo/beginners
>     _______________________________________________
>     Beginners mailing list
>     [email protected] <mailto:[email protected]>
>     http://www.haskell.org/mailman/listinfo/beginners
>
>



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

Message: 2
Date: Wed, 13 Jul 2011 16:31:29 +0000
From: Roelof Wobben <[email protected]>
Subject: Re: [Haskell-beginners] safetail problem
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"


Oke, 

Then I made there a error.
I want to check if xs is a empty list.
So it must be if null xs then [] else xs

Roelof


> Date: Wed, 13 Jul 2011 18:28:40 +0200
> From: [email protected]
> To: [email protected]
> CC: [email protected]; [email protected]
> Subject: Re: safetail problem
> 
> Am 13.07.2011 17:40, schrieb Kyle Murphy:
> > Your new code declares that you're expecting a [x,xs] which isn't a
> > valid type. What you're expecting is a [x]. Furthermore you then try to
> > pattern match against (x,xs) which is a tuple even though your function
> > just declared it's expecting a list. What you want is:
> >
> > safetail :: [x] -> [x]
> > safetail (x:xs) = if null[xs] then [] else xs
> 
> Here I miss an equation:
> 
>    safetail [] = ...
> 
> Also "if null[xs] then [] else xs" happens to correctly simplify to "xs" 
> because "null[xs]" is never True, because "[xs]" is a singleton list (of 
> lists).
> 
> C.
> 
> >
> > (x:xs) is a pattern match using one of the two list constructors (:)
> > which has type:
> >
> > (:) :: a -> [a] -> [a]
> >
> > This means that (:) takes two arguments, anything, and a list of
> > anything, and returns a new list of anything (in this case with the
> > first argument pre-pended to the second argument).
> >
> > The other list constructor ([]) is a zero argument constructor of type:
> >
> > [] :: [a]
> >
> > That is, it can be used to construct an empty list of any type.
> >
> > With these two constructors it's possible to understand how lists
> > working in Haskell.
> > [1,2,3,4] is syntactic sugar (that is, it's replaced by the compiler for
> > you and exists solely for convenience of writing) for 1:2:3:4:[].
> > Following the types you have:
> > [] -> Constructs an empty list of type [a], we don't know what "a" is yet.
> > 4:[] -> Takes an Int (4) and a list [Int] ([]) and constructs a new list
> > of type [Int]. The previous type of [a] is forced to be [Int] at this point.
> > 3:[4] -> Takes an Int (3) and a list ([4]) and returns a new list ([3,4]).
> > 2:[3,4] -> Takes an Int (2) and a list ([3,4]) and returns a new list
> > ([2,3,4]).
> > 1:[2,3,4] -> Takes an Int (1) and a list ([2,3,4]) and returns a new
> > list ([1,2,3,4]).
> >
> > N.B. I'm not an expert on Haskell, so the following might be wrong in
> > some of the details, but is the way I understand lists to function in
> > Haskell.
> >
> > Don't be confused at this point with the empty list constructor [], and
> > the list type ([]), which even though they use the same name are
> > actually two different things.
> > To be clear, the data declaration of List actually looks similar to the
> > following:
> >
> > data ([]) a = [] | (:) a (([]) a)
> >
> > which could also be written as:
> >
> > data [a] = [] | a:[a]
> >
> > The (([]) a) on the left of the equal sign is the Type. The [] on the
> > right of the equal sign is the constructor.
> >
> > -R. Kyle Murphy
> > --
> > Curiosity was framed, Ignorance killed the cat.
> >
> >
> > On Wed, Jul 13, 2011 at 10:58, Roelof Wobben <[email protected]
> > <mailto:[email protected]>> wrote:
> >
> >
> >     With GHCI I get this message :
> >
> >
> >
> >     safetail.hs:1:16: parse error on input ','
> >
> >
> >
> >     Roelof
> >
> >
> >
> >     ----------------------------------------
> >      > From: [email protected] <mailto:[email protected]>
> >      > To: [email protected] <mailto:[email protected]>
> >      > Date: Wed, 13 Jul 2011 14:54:01 +0000
> >      > Subject: Re: [Haskell-beginners] safetail problem
> >      >
> >      >
> >      > Hello,
> >      >
> >      >
> >      >
> >      > I have GHC installed so I will try this one with ghci.
> >      >
> >      >
> >      >
> >      > Roelof
> >      >
> >      >
> >      >
> >      > ----------------------------------------
> >      > > Subject: Re: [Haskell-beginners] safetail problem
> >      > > From: [email protected] <mailto:[email protected]>
> >      > > Date: Wed, 13 Jul 2011 08:10:38 -0400
> >      > > CC: [email protected] <mailto:[email protected]>
> >      > > To: [email protected] <mailto:[email protected]>
> >      > >
> >      > > On Jul 13, 2011, at 3:07 AM, Roelof Wobben wrote:
> >      > >
> >      > > > changed it to this : http://codepad.org/ROV4ASAB
> >      > >
> >      > >
> >      > > Hi, Roelof.
> >      > >
> >      > > Have you installed GHC? You will find the error messages to be
> >     more helpful when you use ghci to execute your code.
> >      > >
> >      > > I don't know Programming in Haskell by Graham Hutton. I am sure
> >     that it very good, but maybe not the best for you. You might also
> >     try this tutorial.
> >      > >
> >      > > > http://learnyouahaskell.com/chapters
> >      > >
> >      > > ____________________
> >      > > David Place
> >      > > Owner, Panpipes Ho! LLC
> >      > > http://panpipesho.com
> >      > > [email protected] <mailto:[email protected]>
> >      > >
> >      > >
> >      > >
> >      > _______________________________________________
> >      > Beginners mailing list
> >      > [email protected] <mailto:[email protected]>
> >      > http://www.haskell.org/mailman/listinfo/beginners
> >     _______________________________________________
> >     Beginners mailing list
> >     [email protected] <mailto:[email protected]>
> >     http://www.haskell.org/mailman/listinfo/beginners
> >
> >
                                          
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110713/297b7e45/attachment-0001.htm>

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

Message: 3
Date: Wed, 13 Jul 2011 18:41:18 +0200
From: Christian Maeder <[email protected]>
Subject: Re: [Haskell-beginners] safetail problem
To: Roelof Wobben <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Am 13.07.2011 18:31, schrieb Roelof Wobben:
> Oke,
>
> Then I made there a error.
> I want to check if xs is a empty list.
> So it must be if null xs then [] else xs

  "if null xs then [] else xs" still identical to "xs"

C.



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

Message: 4
Date: Wed, 13 Jul 2011 13:21:37 -0400
From: Tom Murphy <[email protected]>
Subject: Re: [Haskell-beginners] safetail problem
To: Roelof Wobben <[email protected]>
Cc: [email protected]
Message-ID:
        <CAO9Q0tVTKT2R10nqO5RP+59=1nprjez0sd8ifnwd_aegks+...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

On 7/13/11, Roelof Wobben <[email protected]> wrote:
>
> Oke,
>
> Then I made there a error.
> I want to check if xs is a empty list.
> So it must be if null xs then [] else xs


When you say
safeTail (x:xs) = if null xs then [] else xs

, what you're saying is, "if xs is [], return []. Otherwise, return
xs." In other words, always return xs. That means you're defining
safeTail as "safeTail (x:xs) = xs" which does give the tail, but is
not safe.
     What happens when the whole list is []? (When you call "safeTail []")
     "safeTail (x:xs)" won't match it, because [] doesn't fit the
pattern x:xs (an element added to the "front" of a string).
     In haskell, you can define a function multiple times, for
different patterns. For example, I can say:

f 0 = 0
f x = 10/x

The patterns are tried "top-down" (first, the pattern "0" is matched
against the input. If it doesn't match, haskell tries the next
function definition).

     See if this helps you define a safeTail!

Tom



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

Message: 5
Date: Wed, 13 Jul 2011 17:43:11 +0000
From: Roelof Wobben <[email protected]>
Subject: Re: [Haskell-beginners] safetail problem
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"


Oke, 

I understand it now.
Now a new chapter list compreshions.
I hope I get the hang of that.

Roelof


> Date: Wed, 13 Jul 2011 13:21:37 -0400
> Subject: Re: [Haskell-beginners] safetail problem
> From: [email protected]
> To: [email protected]
> CC: [email protected]
> 
> On 7/13/11, Roelof Wobben <[email protected]> wrote:
> >
> > Oke,
> >
> > Then I made there a error.
> > I want to check if xs is a empty list.
> > So it must be if null xs then [] else xs
> 
> 
> When you say
> safeTail (x:xs) = if null xs then [] else xs
> 
> , what you're saying is, "if xs is [], return []. Otherwise, return
> xs." In other words, always return xs. That means you're defining
> safeTail as "safeTail (x:xs) = xs" which does give the tail, but is
> not safe.
>      What happens when the whole list is []? (When you call "safeTail []")
>      "safeTail (x:xs)" won't match it, because [] doesn't fit the
> pattern x:xs (an element added to the "front" of a string).
>      In haskell, you can define a function multiple times, for
> different patterns. For example, I can say:
> 
> f 0 = 0
> f x = 10/x
> 
> The patterns are tried "top-down" (first, the pattern "0" is matched
> against the input. If it doesn't match, haskell tries the next
> function definition).
> 
>      See if this helps you define a safeTail!
> 
> Tom
                                          
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110713/730326d5/attachment.htm>

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

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


End of Beginners Digest, Vol 37, Issue 25
*****************************************

Reply via email to