
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: Christian.Maeder@dfki.de To: orclev@gmail.com CC: rwobben@hotmail.com; beginners@haskell.org 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
mailto:rwobben@hotmail.com> wrote: With GHCI I get this message :
safetail.hs:1:16: parse error on input ','
Roelof
---------------------------------------- > From: rwobben@hotmail.com mailto:rwobben@hotmail.com > To: beginners@haskell.org mailto:beginners@haskell.org > 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: d@vidplace.com mailto:d@vidplace.com > > Date: Wed, 13 Jul 2011 08:10:38 -0400 > > CC: beginners@haskell.org mailto:beginners@haskell.org > > To: rwobben@hotmail.com mailto:rwobben@hotmail.com > > > > 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 > > d@vidplace.com mailto:d@vidplace.com > > > > > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org mailto:Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners _______________________________________________ Beginners mailing list Beginners@haskell.org mailto:Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners