[Haskell] Haskell [x] and x notation - as-pattern example
Brandon Allbery
allbery.b at gmail.com
Wed Apr 3 15:39:38 CEST 2013
On Wed, Apr 3, 2013 at 5:01 AM, Angus Comber <anguscomber at gmail.com> wrote:
> In the (x:xs) : just delimits each element. so x is the first element. Why
> can I not print by using x?
>
> Also xs is of what type? list of values? So does this mean x is an element
> and xs must be of type list? Confused...
>
Actually, you just answered yourself. x is an element, xs is a list. (++)
combines lists, so to insert your element using (++) you need to make it a
list. [x] is a list containing your element x and nothing else.
Another way to do it is to do the same as the pattern match, but in this
case that's kinda ugly:
firstLetter all@(x:xs) = "The first letter of " ++ all ++ " is " ++ (x
: " otherbit ") ++ xs
or
firstLetter all@(x:xs) = "The first letter of " ++ all ++ " is " ++ (x
: []) ++ " otherbit " ++ xs
(note that "is of type list" is incomplete; list of *what*? In this case,
list of Char. Haskell String is just [Char] (list of Char), which is highly
convenient but a bit slow in real programs that manipulate a lot of
String-s.)
--
brandon s allbery kf8nh sine nomine associates
allbery.b at gmail.com ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell/attachments/20130403/44f34728/attachment.htm>
More information about the Haskell
mailing list