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.  Question on : usage (Angus Comber)
   2. Re:  Question on : usage (Brandon Allbery)
   3. Re:  Question on : usage (akash g)
   4. Re:  Question on : usage (Daniel Trstenjak)
   5. Re:  Question on : usage (Michael Orlitzky)
   6.  Question on (x:xs) form (Angus Comber)


----------------------------------------------------------------------

Message: 1
Date: Mon, 23 Dec 2013 13:55:12 +0000
From: Angus Comber <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: [Haskell-beginners] Question on : usage
Message-ID:
        <caatguhwbe-os+bem3xnac2pgo52xxe7g2pdjq0rxootkr+k...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

I am playing with concatenating lists so I start with this:

testconcat :: [[a]] -> [a]
testconcat [[]] = []
testconcat [(x:xs)] = x : xs

Now, testconcat [[]] and testconcat [[1,2]] works

So now I want to try with a 2nd inner list so I do this:

testconcat :: [[a]] -> [a]
testconcat [[]] = []
testconcat [(x:xs)] = x : xs
testconcat [(x:xs), (y:ys)] = x : xs : y : ys

and I get load error:
    Couldn't match expected type `a' with actual type `[a]'
      `a' is a rigid type variable bound by
          the type signature for testconcat :: [[a]] -> [a]
          at prog_haskell.hs:218:15
    In the first argument of `(:)', namely `xs'
    In the second argument of `(:)', namely `xs : y : ys'
    In the expression: x : xs : y : ys


But this loads ok:
testconcat :: [[a]] -> [a]
testconcat [[]] = []
testconcat [(x:xs)] = x : xs
testconcat [(x:xs), (y:ys)] = x : xs

Even this line cannot be loaded:
testconcat [(x:xs), (y:ys)] = x : xs : []

    Couldn't match expected type `a' with actual type `[a]'
      `a' is a rigid type variable bound by
          the type signature for testconcat :: [[a]] -> [a]
          at prog_haskell.hs:218:15
    In the first argument of `(:)', namely `xs'
    In the second argument of `(:)', namely `xs : []'
    In the expression: x : xs : []
Failed, modules loaded: none.

if x : xs works why not x : xs : <something else> ???

Can someone please explain?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20131223/94f5b1fa/attachment-0001.html>

------------------------------

Message: 2
Date: Mon, 23 Dec 2013 09:03:46 -0500
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Question on : usage
Message-ID:
        <CAKFCL4Xz-KyifWfy0Nd1u720ogiE+ZEXpMfE=Q5=ka+xhn6...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Mon, Dec 23, 2013 at 8:55 AM, Angus Comber <[email protected]> wrote:

>     Couldn't match expected type `a' with actual type `[a]'
>       `a' is a rigid type variable bound by
>           the type signature for testconcat :: [[a]] -> [a]
>           at prog_haskell.hs:218:15
>     In the first argument of `(:)', namely `xs'
>     In the second argument of `(:)', namely `xs : []'
>     In the expression: x : xs : []
> Failed, modules loaded: none.
>
> if x : xs works why not x : xs : <something else> ???
>

x is an element.
xs is a list of those elements.
x : xs prepends a single element to a list.
x : xs : y would attempt to prepend a single element *and* a list to
something else --- but if you deconstructed a list to get x and xs, then x
and xs are not the same type (since xs's type is that of a list of x).

Perhaps the thing to understand is that in Haskell, a list is built up of
elements all of the same type using (:):

[x,y,z]   is the same as   x : y : z : []

(If you're familiar with Lisp, (:) is exactly cons and [] is nil.)

But because Haskell is strictly typed, a list must contain elements all the
same type. So you can't have y in that be a list of the same type as x, it
must be a value the same type as x.

Maybe you are looking for (++) instead? But note that that takes lists, not
items, so it would have to be ([x] ++ xs ++ ...).

-- 
brandon s allbery kf8nh                               sine nomine associates
[email protected]                                  [email protected]
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20131223/66ee5b80/attachment-0001.html>

------------------------------

Message: 3
Date: Mon, 23 Dec 2013 19:35:54 +0530
From: akash g <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Question on : usage
Message-ID:
        <caliga_cyhmcjznz-rrq_y66xk8wq0jrqhuaovwpg5abvsgt...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Because the type of cons (:) is a -> [a] -> [a].
Thus, when you do list : something else, you get a type mismatch.  You are
basically giving it [a] -> [a] -> [a].
You got yourself a type violation.



On Mon, Dec 23, 2013 at 7:25 PM, Angus Comber <[email protected]> wrote:

> I am playing with concatenating lists so I start with this:
>
> testconcat :: [[a]] -> [a]
> testconcat [[]] = []
> testconcat [(x:xs)] = x : xs
>
> Now, testconcat [[]] and testconcat [[1,2]] works
>
> So now I want to try with a 2nd inner list so I do this:
>
> testconcat :: [[a]] -> [a]
> testconcat [[]] = []
> testconcat [(x:xs)] = x : xs
> testconcat [(x:xs), (y:ys)] = x : xs : y : ys
>
> and I get load error:
>     Couldn't match expected type `a' with actual type `[a]'
>       `a' is a rigid type variable bound by
>           the type signature for testconcat :: [[a]] -> [a]
>           at prog_haskell.hs:218:15
>     In the first argument of `(:)', namely `xs'
>     In the second argument of `(:)', namely `xs : y : ys'
>     In the expression: x : xs : y : ys
>
>
> But this loads ok:
> testconcat :: [[a]] -> [a]
> testconcat [[]] = []
> testconcat [(x:xs)] = x : xs
> testconcat [(x:xs), (y:ys)] = x : xs
>
> Even this line cannot be loaded:
> testconcat [(x:xs), (y:ys)] = x : xs : []
>
>     Couldn't match expected type `a' with actual type `[a]'
>       `a' is a rigid type variable bound by
>           the type signature for testconcat :: [[a]] -> [a]
>           at prog_haskell.hs:218:15
>     In the first argument of `(:)', namely `xs'
>     In the second argument of `(:)', namely `xs : []'
>     In the expression: x : xs : []
> Failed, modules loaded: none.
>
> if x : xs works why not x : xs : <something else> ???
>
> Can someone please explain?
>
>
> _______________________________________________
> 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/20131223/c4ccc8a6/attachment-0001.html>

------------------------------

Message: 4
Date: Mon, 23 Dec 2013 15:11:08 +0100
From: Daniel Trstenjak <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Question on : usage
Message-ID: <20131223141108.GA10571@machine>
Content-Type: text/plain; charset=us-ascii


Hi Angus,

> testconcat [(x:xs), (y:ys)] = x : xs : y : ys

When you're replacing the x, xs, y, and ys with types you're getting:

   a : [a] : a : [a]

The type of (:) is:

   a -> [a] -> [a]

Can you see the problem? Hint, the problem is the (:) in the middle.


Greetings,
Daniel


------------------------------

Message: 5
Date: Mon, 23 Dec 2013 09:12:25 -0500
From: Michael Orlitzky <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Question on : usage
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On 12/23/2013 08:55 AM, Angus Comber wrote:
> I am playing with concatenating lists so I start with this:
> 
> testconcat :: [[a]] -> [a]
> testconcat [[]] = []
> testconcat [(x:xs)] = x : xs
> 
> Now, testconcat [[]] and testconcat [[1,2]] works
> 
> So now I want to try with a 2nd inner list so I do this:
> 
> testconcat :: [[a]] -> [a]
> testconcat [[]] = []
> testconcat [(x:xs)] = x : xs
> testconcat [(x:xs), (y:ys)] = x : xs : y : ys
> 
> and I get load error:
>     Couldn't match expected type `a' with actual type `[a]'
>       `a' is a rigid type variable bound by
>           the type signature for testconcat :: [[a]] -> [a]
>           at prog_haskell.hs:218:15
>     In the first argument of `(:)', namely `xs'
>     In the second argument of `(:)', namely `xs : y : ys'
>     In the expression: x : xs : y : ys
> 

The colon operator (:) takes a single element on the left, and a list on
the right. In,

  testconcat [(x:xs), (y:ys)] = x : xs : y : ys

you've got lists on both the left and the right. This would work:

  testconcat [(x:xs), (y:ys)] = x : ( y : (xs ++ ys) )

(note: not the same function as you wanted!) since it keeps all of the
single-elements on the left-hand side of (:). You're going to need to
use (++) in at least one place, though, since you need to combine the
lists 'xs' and 'ys' somehow. The function you really want is,

  testconcat [(x:xs), (y:ys)] = (x:xs) ++ (y:ys)

but of course this is just,

  testconcat [l1, l2] = l1 ++ l2



------------------------------

Message: 6
Date: Mon, 23 Dec 2013 14:57:35 +0000
From: Angus Comber <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: [Haskell-beginners] Question on (x:xs) form
Message-ID:
        <caatguhua3mf0gex0mcmamtdpdp7ovs3pwkbpjyk2ins4qcj...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Eg for a definition of reverse:

reverse' :: [a] -> [a]
reverse' [] = []
reverse' (x:xs) = reverse' xs ++ [x]

In the last line of the definition, x is an element in the list (the first
element) and xs represents the remainder of the list.

so if list was [1,2,3] then x is 1 and xs is [2,3]

Why are the brackets required?  And what do they signify?

Eg reverse' x:xs = reverse' xs ++ [x] results in a parse error.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20131223/9eb534c6/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 66, Issue 16
*****************************************

Reply via email to