[Haskell] Haskell [x] and x notation - as-pattern example

Angus Comber anguscomber at gmail.com
Wed Apr 3 15:42:31 CEST 2013


Yes it seems that ++ concatenates lists not elements and that was the
underlying problem.

I have subscribed to the beginners list - didn't know there was one for
people like me just starting to walk :)


On 3 April 2013 14:39, Brandon Allbery <allbery.b at gmail.com> wrote:

> 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/2ca8c14b/attachment.htm>


More information about the Haskell mailing list