Thread View: comp.lang.functional
6 messages
6 total messages
Started by Konrad Den Ende
Mon, 17 Feb 2003 18:57
How do i approach this problem?
Author: Konrad Den Ende
Date: Mon, 17 Feb 2003 18:57
Date: Mon, 17 Feb 2003 18:57
38 lines
2921 bytes
2921 bytes
I have a very strong feeling that i already asked the question i'm
about to ask now but failed to comprehend or memorize the answer.
Therefore i appologize in advance if anybody already has been kind
enough to explain this to me before. Please share your wisdom again :)
I can build a list by [xs | xs <- [[1,2,3],[4,5,6]]] and am happy with the
outcome. Now what i'd like to do is being able to process xs BEFORE it
"hits the market". I've tried among many things (including screaming
loudly at my computer and kicking a garbage can) this:
[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 first
element of the first element, and then put them togehter to form a new
list". 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.
I ment the outcome to be [[1,1,2,3],[4,4,5,6]] and i have two questions.
1) What should i do to trick my computer into doing what i want?
2) What does the stupid machine think i was trying to tell it to do?
If possible, please adjust the answer to a level suitable for a banana or
any other object with IQ not greater than that.
--
V�nligen
Konrad
---------------------------------------------------
phone #1: (+46/0) 708 - 70 73 92
phone #2: (+46/0) 704 - 79 96 95
url: http://konrads.webbsida.com
e-mail: chamsterkonrad@bigfoot.com
-----------------------------------
Sleep - thing used by ineffective people
as a substitute for coffee
Ambition - a poor excuse for not having
enough sence to be lazy
---------------------------------------------------
Re: How do i approach this problem?
Author: nospm
Date: Mon, 17 Feb 2003 19:44
Date: Mon, 17 Feb 2003 19:44
15 lines
1467 bytes
1467 bytes
In article <b2rbba$6dc$1@news.gu.se>, "Konrad Den Ende" <chamsterkonrad@bigfoot.com> writes: > ... many whitespace > loudly at my computer and kicking a garbage can) this: > [x:xs | xs <- [[1,2,3],[4,5,6]], x <- head xs] > ... > I ment the outcome to be [[1,1,2,3],[4,4,5,6]] and i have two questions. ever tried: Prelude> map (\x->head x:x) [xs | xs <-[[1,2,3],[4,5,6]] ] [[1,1,2,3],[4,4,5,6]] hope it would help :) -Philip
Re: How do i approach this problem?
Author: Sebastian Sylvan
Date: Mon, 17 Feb 2003 20:20
Date: Mon, 17 Feb 2003 20:20
63 lines
3776 bytes
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.| | | <=================================> | ----------------------------------------
Re: How do i approach this problem?
Author: Konrad Den Ende
Date: Mon, 17 Feb 2003 23:06
Date: Mon, 17 Feb 2003 23:06
38 lines
2550 bytes
2550 bytes
OK, got it. Love it. Mucho Thanko to both!
Now, i have wasted several layers of my keyboard to produce following
amazing decencies:
[ x:xs | xs <- ["123","456"] , x <- [head xs] ]
It works as i wish and special thanks to mr. Sebastian S for pointing out
that "head xs" is not a list, unless made being one.
What i don't like is that the following doesn't work:
[ x:xs | x <- [head xs] , xs <- ["123","456"] ]
since i remember reading somewhere that Hasell doesn't have variables,
but rather constants that get a value assigned upon execution, thus keeping
it to the end of the time. Correct? If so, why the error. If no, who to
kill?
As it is now, i refer to xs when defining the list from which x is to be
taken
from. Nice and neat. What should i do if i'd like to refer to x when
defining
the list from which xs are to be taken? Is it possible at all ?
--
V�nligen
Konrad
---------------------------------------------------
phone #1: (+46/0) 708 - 70 73 92
phone #2: (+46/0) 704 - 79 96 95
url: http://konrads.webbsida.com
e-mail: chamsterkonrad@bigfoot.com
-----------------------------------
Sleep - thing used by ineffective people
as a substitute for coffee
Ambition - a poor excuse for not having
enough sence to be lazy
---------------------------------------------------
Re: How do i approach this problem?
Author: Sebastian Sylvan
Date: Mon, 17 Feb 2003 23:35
Date: Mon, 17 Feb 2003 23:35
43 lines
3150 bytes
3150 bytes
On Tue, 18 Feb 2003, Konrad Den Ende wrote: > Now, i have wasted several layers of my keyboard to produce following > amazing decencies: > [ x:xs | xs <- ["123","456"] , x <- [head xs] ] > It works as i wish and special thanks to mr. Sebastian S for pointing out > that "head xs" is not a list, unless made being one. That really isn't a nice way to write it =) Why use the "drawn from" syntax on a list containing one element. Just do: [ head xs : xs | xs <- ["123","456"] ] > What i don't like is that the following doesn't work: > [ x:xs | x <- [head xs] , xs <- ["123","456"] ] > since i remember reading somewhere that Hasell doesn't have variables, > but rather constants that get a value assigned upon execution, thus keeping > it to the end of the time. Correct? If so, why the error. If no, who to > kill? I _think_ that list comprehensions are just syntactic sugar for the list monad. Don't worry about that now, just remember that the lists are "traversed" from left to right. Which means that the above says "for each x in the list [head xs], take each xs in ["123","456"] and do x:xs". Now this doesn't make sense because the list that x is drawn from is defined using something which isn't defined yet! The qualifiers in the list comprehensions must be written down left to right, because they "variables" are really just short hands for "every element in the list". This is evaluated left to right so you can define a list using a variable that is drawn from a list to the left of it. -- ---------------------------------------- | 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.| | | <=================================> | ----------------------------------------
Re: How do i approach this problem?
Author: Ketil Malde
Date: Tue, 18 Feb 2003 06:27
Date: Tue, 18 Feb 2003 06:27
19 lines
1646 bytes
1646 bytes
nospm@uni-koblenz.de writes:
> In article <b2rbba$6dc$1@news.gu.se>, "Konrad Den Ende" <chamsterkonrad@bigfoot.com> writes:
>> [x:xs | xs <- [[1,2,3],[4,5,6]], x <- head xs]
>> I ment the outcome to be [[1,1,2,3],[4,4,5,6]] and i have two questions.
> Prelude> map (\x->head x:x) [xs | xs <-[[1,2,3],[4,5,6]] ]
> [[1,1,2,3],[4,4,5,6]]
How about
map (\(x:xs)->(x:x:xs)) [[1,2,3],[4,5,6]]
i.e. [x | x <- xs] is equivalent to xs.
-kzm
--
If I haven't seen further, it is by standing in the footprints of giants
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads