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. my own last function (bahad?r altan)
2. Re: my own last function (David McBride)
3. The dot operator. (bahad?r altan)
4. Re: The dot operator. (Antoine Latter)
5. Re: The dot operator. (Kyle Murphy)
6. A question about pattern matching (bahad?r altan)
----------------------------------------------------------------------
Message: 1
Date: Sat, 18 Feb 2012 18:18:24 +0000 (GMT)
From: bahad?r altan <[email protected]>
Subject: [Haskell-beginners] my own last function
To: "[email protected]" <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hello everyone, I have just started studying and started trying to solve 99
questions in haskell.org. However I got stuck even in the first question. The
first question wants me to write my own last function and I wrote this :?
q1mylast :: [x] -> x
q1mylast [a] = if length [a] <=1 then ?a
else ?q1mylast (tail [a])
This program doesn't work and it gives this error :?
Program error: pattern match failure: q1mylast "doaltan"
I'll be very glad if you could tell me what's wrong with the code and how can I
fix it..
Thanks in advance :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120218/4f18670e/attachment-0001.htm>
------------------------------
Message: 2
Date: Sat, 18 Feb 2012 13:37:14 -0500
From: David McBride <[email protected]>
Subject: Re: [Haskell-beginners] my own last function
To: bahad?r altan <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID:
<CAN+Tr43z=ic8w+6qbbe35vz447imwuryyfxu9vrkhysypg2...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
[a] when used as an argument doesn't mean what you think it does. It
means a list with only one element a. So your function will only
match one single case, a list such as [3], or a string with one
character "a".
Here are all the things you could match on:
xs -> just a variable which will contain the whole list
[] -> an empty list
[a] -> a list with one element in it, which you can reference via a
[a,b,c] -> a list with exactly three elements in it, which can all be referenced
(x:xs) -> a list with at least one element, of which x is that
element, and xs is the rest of the list
(x:[]) -> a list with one element, followed by nothing else.
Essentially the same as [x].
(x:y:xs) -> a list with two elements, xs has the rest of the list.
Remember that xs could be empty.
So think about the problem. If someone passes in a list, it either
has one element, or it has more than one and we want to get to the
last one
last :: [x] -> x
last [x] = x
Well that deals with the easy case. One element, one answer. But if
there is more than one element, then what?
last (_:xs) = last xs
Just discard the first one and run the function again on what remains.
On Sat, Feb 18, 2012 at 1:18 PM, bahad?r altan <[email protected]> wrote:
> Hello everyone, I have just started studying and started trying to solve 99
> questions in haskell.org. However I got stuck even in the first question.
> The first question wants me to write my own last function and I wrote this
> :
>
> q1mylast :: [x] -> x
> q1mylast [a] = if length [a] <=1 then ?a
> else ?q1mylast (tail [a])
>
> This program doesn't work and it gives this error :
>
> Program error: pattern match failure: q1mylast "doaltan"
>
> I'll be very glad if you could tell me what's wrong with the code and how
> can I fix it..
> Thanks in advance :)
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 3
Date: Sat, 18 Feb 2012 19:33:16 +0000 (GMT)
From: bahad?r altan <[email protected]>
Subject: [Haskell-beginners] The dot operator.
To: "[email protected]" <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hello. I saw the dot "." operator in some Haskell codes, but I couldn't figure
out what it does. I didn't see it in some books and tutorials and couldn't find
an explanation on the Internet. Could you tell me what it does?
Thanks:)
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120218/5cf0fcd6/attachment-0001.htm>
------------------------------
Message: 4
Date: Sat, 18 Feb 2012 13:44:22 -0600
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] The dot operator.
To: bahad?r altan <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID:
<CAKjSnQFNv5f=yzatt6ipmnfzvrrb8bv77pvm8cjewcdqhe0...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
On Sat, Feb 18, 2012 at 1:33 PM, bahad?r altan <[email protected]> wrote:
> Hello. I saw the dot "." operator in some Haskell codes, but I couldn't
> figure out what it does. I didn't see it in some books and tutorials and
> couldn't find an explanation on the Internet. Could you tell me what it
> does?
>
> Thanks:)
>
Since it is an ordinary haskell function, it is something you can look
up with Hoogle:
http://www.haskell.org/hoogle/?hoogle=%28.%29
It is used for function composition - for example if you wrote a function:
> myFunc x = func1 (func2 x)
you can re-write this to:
> myFunc = func1 . func2
It's useful to cut down on parenthesis when you have a chain of
functions taking one argument.
Antoine
------------------------------
Message: 5
Date: Sat, 18 Feb 2012 14:47:49 -0500
From: Kyle Murphy <[email protected]>
Subject: Re: [Haskell-beginners] The dot operator.
To: bahad?r altan <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID:
<CA+y6JcyCXeKWhhwdN=k=gpaqobzkky-8mhyvea47aplapxm...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
It's function composition.
(.) :: (b -> c) -> (a -> b) -> (a -> c)
E.G. putStrLn . show $ 42
-R. Kyle Murphy
Sent from my phone.
On Feb 18, 2012 2:34 PM, "bahad?r altan" <[email protected]> wrote:
> Hello. I saw the dot "." operator in some Haskell codes, but I couldn't
> figure out what it does. I didn't see it in some books and tutorials and
> couldn't find an explanation on the Internet. Could you tell me what it
> does?
>
> Thanks:)
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120218/9d1d05b6/attachment-0001.htm>
------------------------------
Message: 6
Date: Sat, 18 Feb 2012 20:16:05 +0000 (GMT)
From: bahad?r altan <[email protected]>
Subject: [Haskell-beginners] A question about pattern matching
To: "[email protected]" <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hello. I'm wondering what " (Eq a) =>" part in the code below does.. I couldn't
figure it myself.. Thanks in advance :)
isPalindrome :: (Eq a) => [a] -> Bool isPalindrome xs = xs == (reverse xs)
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120218/9dd67659/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 44, Issue 18
*****************************************