
Hello, As a exercise I need to rewrite the tail function into a safetail function by using conditions. So I made this : http://codepad.org/bKcCUdqy But as you can see there is a error message. Can anyone explain me how to solve this ? Roelof

_ isn't a value: it's a wildcard character for pattern matching.
Also, if x = [1,2,3], [0,x] is the wrong way to make a list. The right
way is (0:x) (":" is "cons")
What resource are you learning from?
On 7/12/11, Roelof Wobben
Hello,
As a exercise I need to rewrite the tail function into a safetail function by using conditions.
So I made this : http://codepad.org/bKcCUdqy
But as you can see there is a error message.
Can anyone explain me how to solve this ?
Roelof
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hello, I use Programming in Haskell from Graham Hutton. But I can't find anything in the first 4 chapters from that book why I must use (0,x) en what that means. Roelof ----------------------------------------
Date: Tue, 12 Jul 2011 15:21:46 -0400 Subject: Re: [Haskell-beginners] safetail problem From: amindfv@gmail.com To: rwobben@hotmail.com CC: beginners@haskell.org
_ isn't a value: it's a wildcard character for pattern matching.
Also, if x = [1,2,3], [0,x] is the wrong way to make a list. The right way is (0:x) (":" is "cons")
What resource are you learning from?
On 7/12/11, Roelof Wobben
wrote: Hello,
As a exercise I need to rewrite the tail function into a safetail function by using conditions.
So I made this : http://codepad.org/bKcCUdqy
But as you can see there is a error message.
Can anyone explain me how to solve this ?
Roelof
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

If:
x = [1,2,3] :: [Int]
y = [0] :: [Int]
z = 0
then:
[y,x] == [[0],[1,2,3]] :: [[Int]]
- a list of lists. It doesn't get simplified into [0,1,2,3].
(z,x) == (0,[1,2,3]) :: (Int, [Int])
- a tuple, whose first element is an Int, and whose second element
is a list of Ints ([Int])
z:x == [0,1,2,3] :: [Int]
The third one is what you're looking for. It's called "cons."
Tom
On 7/12/11, Roelof Wobben
Hello,
I use Programming in Haskell from Graham Hutton.
But I can't find anything in the first 4 chapters from that book why I must use (0,x) en what that means.
Roelof
----------------------------------------
Date: Tue, 12 Jul 2011 15:21:46 -0400 Subject: Re: [Haskell-beginners] safetail problem From: amindfv@gmail.com To: rwobben@hotmail.com CC: beginners@haskell.org
_ isn't a value: it's a wildcard character for pattern matching.
Also, if x = [1,2,3], [0,x] is the wrong way to make a list. The right way is (0:x) (":" is "cons")
What resource are you learning from?
On 7/12/11, Roelof Wobben
wrote: Hello,
As a exercise I need to rewrite the tail function into a safetail function by using conditions.
So I made this : http://codepad.org/bKcCUdqy
But as you can see there is a error message.
Can anyone explain me how to solve this ?
Roelof
_______________________________________________ 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

On Jul 12, 2011, at 4:45 PM, Roelof Wobben wrote:
But I can't find anything in the first 4 chapters from that book why I must use (0,x) en what that means.
I think lists can be confusing for someone who is trying to learn Haskell and has only used imperative languages before. Typically, those languages use arrays to manipulate lists of things. The meaning of lists in Haskell is hidden by the confusing but convenient syntactic sugar used to write them. Really, lists are a recursive data type with two cases. The basis case: the empty list (written as "[]", close square bracket then open square bracket) The inductive case: a : List (here ":" is the constructor) So, if we see a list written as [1,2,3,4] it really means (1 : (2 : (3 : (4 : [])))) All the programs that work on lists use the underlying representation, so you really need to understand it. This confusion goes all the way back to 1960. Blame it on John McCarthy, inventor of Lisp! I hope that is a little helpful. Cheers, David ____________________ David Place Owner, Panpipes Ho! LLC http://panpipesho.com d@vidplace.com

Oke, I changed it to this : http://codepad.org/ROV4ASAB But still there are errors. Roelof ----------------------------------------
Date: Wed, 13 Jul 2011 00:33:57 +0200 From: haskell@phirho.com To: beginners@haskell.org Subject: Re: [Haskell-beginners] safetail problem
On 12.07.2011 22:45, Roelof Wobben wrote:
Hello, I use Programming in Haskell from Graham Hutton. But I can't find anything in the first 4 chapters from that book why I must use (0,x) en what that means.
FWIW: chapter 4.4, page 33.
Regards, Thomas
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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.
____________________ David Place Owner, Panpipes Ho! LLC http://panpipesho.com d@vidplace.com

Hello, I have GHC installed so I will try this one with ghci. Roelof ----------------------------------------
Subject: Re: [Haskell-beginners] safetail problem From: d@vidplace.com Date: Wed, 13 Jul 2011 08:10:38 -0400 CC: beginners@haskell.org To: 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.
____________________ David Place Owner, Panpipes Ho! LLC http://panpipesho.com d@vidplace.com

With GHCI I get this message : safetail.hs:1:16: parse error on input ',' Roelof ----------------------------------------
From: rwobben@hotmail.com To: 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 Date: Wed, 13 Jul 2011 08:10:38 -0400 CC: beginners@haskell.org To: 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.
____________________ David Place Owner, Panpipes Ho! LLC http://panpipesho.com d@vidplace.com
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

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
(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
With GHCI I get this message :
safetail.hs:1:16: parse error on input ','
Roelof
----------------------------------------
From: rwobben@hotmail.com To: 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 Date: Wed, 13 Jul 2011 08:10:38 -0400 CC: beginners@haskell.org To: 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.
____________________ David Place Owner, Panpipes Ho! LLC http://panpipesho.com d@vidplace.com
_______________________________________________ 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

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

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

On 7/13/11, 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
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

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: amindfv@gmail.com To: rwobben@hotmail.com CC: beginners@haskell.org
On 7/13/11, Roelof Wobben
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

Roelof Wobben
As a exercise I need to rewrite the tail function into a safetail function by using conditions.
So I made this : http://codepad.org/bKcCUdqy
But as you can see there is a error message.
Can anyone explain me how to solve this ?
Haskell is all about types. To understand Haskell, you really must understand how types work. The square brackets are a syntactic construct to easy constructing lists. As David pointed out, the underlying representation is different. If the types don't make sense, the whole program makes no sense. Let's see: [1, [2, 3, 4]] This cannot be right, because lists in Haskell are homogenous, so all elements have the same type, but here you have a list of two elements, one being 1 (a number) and the other being [2, 3, 4] (a list of numbers). The types obviously don't fit, so the program cannot be compiled. You have this: x = 1 y = [2, 3, 4] To put the element x in front of the list of elements y, you need a function of the following type: a -> [a] -> [a] The (:) function has exactly that type, so you can write: (:) x y or to use the usually more convenient infix syntax: x : y This is about list construction. What you need here however is the other direction: destruction. This is done by pattern matching. Again as David pointed out a list is exactly a chain of (:) applications. The list y would have the following real representation: y = 2 : (3 : (4 : [])) Since (:) is right-associative you can leave out the parentheses to have a slightly more readable equation. You can decompose this list by pattern matching, which gives you access to the building blocks of the list: safeTail (x:xs) = xs The (:) function is the "cons" or "prepended to" function. So safeTail applied to x prepended to xs is equal to xs. One more equation and your safeTail function is done. One further note: I have intentionally left out almost all of the type signatures here, but I strongly advise you to write them. In fact write type signatures before writing the actual functions. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/
participants (7)
-
Christian Maeder
-
David Place
-
Ertugrul Soeylemez
-
Kyle Murphy
-
Roelof Wobben
-
Thomas
-
Tom Murphy