🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.functional
Article #1096

Re: How do i approach this problem?

#1096
From: Sebastian Sylvan
Date: Mon, 17 Feb 2003 20:20
63 lines
3776 bytes
On Mon, 17 Feb 2003, Konrad Den Ende wrote:
> [x:xs | xs <- [[1,2,3],[4,5,6]], x <- head xs]
> thinking, "hey, we pick the first element of the list and then the
> ...

That's not what you're saying. More like, "for each element in the first
list, go through each element its head..."
This: [ (a,b) | a <- [1,2,3] , b <- ['a','b','c']]
will yield:
[(1,'a'),(1,'b'),(1,'c'),(2,'a'),(2,'b'),(2,'c'),(3,'a'),(3,'b'),(3,'c')]

Try to understand what happens here. It will take each element of the
first list supplied ([1,2,3]) and then for EACH of these elements it will
go through the second list (and for each element of that go through a
third list, if we had one) and then yield an element by combining the two
extracted element at each stage in some way.

Now. What did you want to do, this:
[ x:x:xs | (x:xs) <- [[1,2,3],[4,5,6]] ]

So what you do here is you _pattern match_ on the extraction from the
list, so the head of the first element of the list will be in xs, and the
tail in xs. The entire element will thus be (x:xs). If you want to add the
head of this element to that same element (effectively duplicating the
first element) you just yield "x:(x:xs)" or prettier: "x:x:xs". 

If you want to do it "your" way you can do this as well.
[ head xs : xs | xs <- [[1,2,3],[4,5,6]] ]

Actually, I think I might like that version better =)
This one reads "for all xs in [[1,2,3],[4,5,6]] take the head of xs and
put it infront of xs".
Notice how similar this is to mathematical notation. The "<-" operator is
meant to look like that weird "drawn-from" symbol from set theory (looks
kinda like an 'e'). At least I assume it's meant look like that.


> The computer regarded that as stupid and claimed "unification would
> give infinite type", which wasn't any good since i don't even understand
> what that means, so my machine is probably just trying to show-off.
> 

Remember what I said your original version meant? "for each element in the
first list, go through each element its head..."
That's your problem. You do " x <- head xs" which means that you take all
elements in the list "head xs", but "head xs" is a single element, and not
a list!
That's where the compiler started complaining.


--

 ----------------------------------------
|  Sebastian Sylvan                      |
|  ICQ: 44640862                         |
|  Tel: 073-6818655 / 031-812 817        |
|                                        |
|  <=================================>   |
|  |Det finns 10 sorters m�nniskor.  |   |
|  |De som f�rst�r det bin�ra tal-   |   |
|  |systemet och de som inte g�r det.|   |
|  <=================================>   |
 ----------------------------------------
Technical Details
Message-ID: <Pine.GSO.4.10.10302172105080.18198-100000@licia.dtek.chalmers.se>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!uio.no!news.netg.se!dd.chalmers.se!licia.dtek.chalmers.se!sylvan
References: <b2rbba$6dc$1@news.gu.se>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 107f24,a907e0b23c6eca41
X-Google-Attributes: gid107f24,public
X-Google-Thread: 10f6e0,a907e0b23c6eca41
X-Google-Attributes: gid10f6e0,public
X-Google-ArrivalTime: 2003-02-17 12:30:02 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!uio.no!news.netg.se!dd.chalmers.se!licia.dtek.chalmers.se!sylvan
From: Sebastian Sylvan <sylvan@dtek.chalmers.se>
Newsgroups: comp.lang.functional,cth.dtek.general
Subject: Re: How do i approach this problem?
Date: Mon, 17 Feb 2003 21:20:11 +0100
Organization: Chalmers University of Technology, Sweden
Lines: 64
Message-ID: <Pine.GSO.4.10.10302172105080.18198-100000@licia.dtek.chalmers.se>
References: <b2rbba$6dc$1@news.gu.se>
NNTP-Posting-Host: licia.dtek.chalmers.se
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset=ISO-8859-1
Content-Transfer-Encoding: 8BIT
In-Reply-To: <b2rbba$6dc$1@news.gu.se>