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. Re: Padding List with Zeros (Greg)
2. Re: Padding List with Zeros (jean verdier)
3. Re: The cost of generality, or how expensive is realToFrac?
(Daniel Fischer)
4. Re: Padding List with Zeros (Greg)
5. Re: Padding List with Zeros (Ozgur Akgun)
----------------------------------------------------------------------
Message: 1
Date: Wed, 15 Sep 2010 01:43:29 -0700
From: Greg <[email protected]>
Subject: Re: [Haskell-beginners] Padding List with Zeros
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
I think you also need to handle the case where the second parameter to g is
empty (where list_A has non-matching elements after list_B is exhausted).
g (a:as) [] = 0 : g as []
or something similar in between the two existing definitions?
On Sep 15, 2010, at 1:34 AM, jean verdier wrote:
>
>
> list_A = [0,10,20,30,40,50]
> list_B = [0,10,50]
> list_C = [2,1,-5]
>
> f a b c = g a (zip b c)
>
> g [] _ = []
> g (a:as) xs@((b,c):xs')
> | a > b = error "b is not a subset of a"
> | a == b = c : g as xs'
> | a < b = 0 : g as xs
>
> main = do
> print (f list_A list_B list_C)
>
>
>
> On Wed, 2010-09-15 at 09:28 +0200, Lorenzo Isella wrote:
>> Hi Antoine,
>> Unfortunately these are really truly lists and not sets (for instance,
>> the ordering of elements matter and some of them may be repeated).
>>
>> Lorenzo
>>
>> On 09/15/2010 01:55 AM, Antoine Latter wrote:
>>> Are these truly lists, or would you be better suited using Sets, Maps or
>>> IntMaps?
>>>
>>> Then you can use some of the unionWith functions to decide what to
>>> insert, or you can simply wrap the looking functions to return zero on
>>> failure.
>>>
>>
>>
>>
>>> Antoine
>>>
>>> On Sep 14, 2010 6:35 PM, "Lorenzo Isella" <[email protected]
>>> <mailto:[email protected]>> wrote:
>>>> Dear All,
>>>> I still have to find my way with immutable lists and list comprehension.
>>>> Consider the following lists
>>>>
>>>> A=[0,10,20,30,40,50]
>>>> B=[0,10,50] (i.e. B is a subset of list A; list A is already ordered in
>>>> increasing order and so is B).
>>>> C=[2,1,-5] i.e. there is a corresponding element in C for every element
>>>> in B.
>>>>
>>>> Now, I would like to define a new list D having length equal to the
>>>> length of A. The elements of D in the position of the elements of A in
>>>> common with B are equal to the corresponding entries in C, whereas the
>>>> other ones are zero i.e.
>>>> D=[2,1,0,0,0,-5]. How can I achieve that? The first thought that comes
>>>> to my mind is to define a list of zeros which I would modify according
>>>> to my needs, but that is not allowed...
>>>> Many thanks
>>>>
>>>> Lorenzo
>>>> _______________________________________________
>>>> Beginners mailing list
>>>> [email protected] <mailto:[email protected]>
>>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 2
Date: Wed, 15 Sep 2010 10:54:40 +0200
From: jean verdier <[email protected]>
Subject: Re: [Haskell-beginners] Padding List with Zeros
To: Greg <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="UTF-8"
You're right, i didn't compile with -fwarn-incomplete-patterns and it
bite me.
You may also write
g as [] = map (\_ -> 0) as
or some combination of take length and repeat.
g as [] = take (length as) (repeat 0)
I'm sorry to have posted incomplete and uncommented code, I should think
twice next time.
On Wed, 2010-09-15 at 01:43 -0700, Greg wrote:
> I think you also need to handle the case where the second parameter to g is
> empty (where list_A has non-matching elements after list_B is exhausted).
>
> g (a:as) [] = 0 : g as []
>
> or something similar in between the two existing definitions?
>
>
> On Sep 15, 2010, at 1:34 AM, jean verdier wrote:
>
> >
> >
> > list_A = [0,10,20,30,40,50]
> > list_B = [0,10,50]
> > list_C = [2,1,-5]
> >
> > f a b c = g a (zip b c)
> >
> > g [] _ = []
> > g (a:as) xs@((b,c):xs')
> > | a > b = error "b is not a subset of a"
> > | a == b = c : g as xs'
> > | a < b = 0 : g as xs
> >
> > main = do
> > print (f list_A list_B list_C)
> >
> >
> >
> > On Wed, 2010-09-15 at 09:28 +0200, Lorenzo Isella wrote:
> >> Hi Antoine,
> >> Unfortunately these are really truly lists and not sets (for instance,
> >> the ordering of elements matter and some of them may be repeated).
> >>
> >> Lorenzo
> >>
> >> On 09/15/2010 01:55 AM, Antoine Latter wrote:
> >>> Are these truly lists, or would you be better suited using Sets, Maps or
> >>> IntMaps?
> >>>
> >>> Then you can use some of the unionWith functions to decide what to
> >>> insert, or you can simply wrap the looking functions to return zero on
> >>> failure.
> >>>
> >>
> >>
> >>
> >>> Antoine
> >>>
> >>> On Sep 14, 2010 6:35 PM, "Lorenzo Isella" <[email protected]
> >>> <mailto:[email protected]>> wrote:
> >>>> Dear All,
> >>>> I still have to find my way with immutable lists and list comprehension.
> >>>> Consider the following lists
> >>>>
> >>>> A=[0,10,20,30,40,50]
> >>>> B=[0,10,50] (i.e. B is a subset of list A; list A is already ordered in
> >>>> increasing order and so is B).
> >>>> C=[2,1,-5] i.e. there is a corresponding element in C for every element
> >>>> in B.
> >>>>
> >>>> Now, I would like to define a new list D having length equal to the
> >>>> length of A. The elements of D in the position of the elements of A in
> >>>> common with B are equal to the corresponding entries in C, whereas the
> >>>> other ones are zero i.e.
> >>>> D=[2,1,0,0,0,-5]. How can I achieve that? The first thought that comes
> >>>> to my mind is to define a list of zeros which I would modify according
> >>>> to my needs, but that is not allowed...
> >>>> Many thanks
> >>>>
> >>>> Lorenzo
> >>>> _______________________________________________
> >>>> Beginners mailing list
> >>>> [email protected] <mailto:[email protected]>
> >>>> http://www.haskell.org/mailman/listinfo/beginners
> >>>
> >>
> >> _______________________________________________
> >> Beginners mailing list
> >> [email protected]
> >> http://www.haskell.org/mailman/listinfo/beginners
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 3
Date: Wed, 15 Sep 2010 10:56:13 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] The cost of generality, or how
expensive is realToFrac?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
On Wednesday 15 September 2010 02:51:01, Greg wrote:
> First, to anyone who recognizes me by name, thanks to the help I've been
> getting here I've managed to put together a fairly complex set of code
> files that all compile together nicely, run, and do exactly what I
> wanted them to do. Â Success!
>
> The trouble is that my implementation is dog slow
>
> Fortunately, this isn't the first time I've been in over my head and I
> started by putting up some simpler scaffolding- which runs much more
> quickly. Â Working backwards, it looks like the real bottle neck is in
> the data types I've created, the type variables I've introduced, and the
> conversion code I needed to insert to make it all happy.
>
> I'm not sure it helps, but I've attached a trimmed down version of the
> relevant code.  What should be happening is my pair  is being converted
> to the canonical form for Coord2D which is Cartesian2D and then
> converted again to Vertex2. Â There shouldn't be any change made to the
> values, they're only being handed from one container to another in this
> case (Polar coordinates would require computation, but I've stripped
> that out for the time being). Â However, those handoffs require calls to
> realToFrac to make the type system happy, and that has to be what is
> eating up all my CPU.
Not all, but probably a big chunk of it.
The problem is that the default implementation of realToFrac is
realToFrac = fromRational . toRational
a) with that implementation, realToFrac :: Double -> Double is not the
identity (doesn't respect NaNs)
b) it's slow, there are no special operations to convert Double, Float etc.
from/to Rational.
For a lot of types, GHC provides rewrite rules (you need to compile with
optimisations to have them fire) which give faster versions (with somewhat
different behaviour, e.g. realToFrac :: Double -> Double is rewritten to
id, realToFrac between Float and Double uses primitive widening/narrowing
ops, for several newtype wrappers around Float/Double there are rules too).
>
> I think there are probably 4 calls to realToFrac. Â If I walk through the
> Â code, the result, given the pair p, should be: Vertex2 (realToFrac
> (realToFrac (fst p))) Â (realToFrac (realToFrac (snd p)))
>
> I'd like to maintain type independence if possible, but I expect most
> uses of this code to feed Doubles in for processing and probably feed
> GLclampf (Floats, I believe)
newtype wrapper around CFloat, which is a newtype wrapper around Float
Unfortunately, there are no rewrite rules in the module where it is
defined, apparently neither any other module that has access to the
constructor. And the constructor is not accessible from any of the exposed
modules, so as far as I know, you can't provide your own rewrite rules.
> to the OpenGL layer. Â If there's a way to
> do so, I wouldn't mind optimizing for that particular set of types.
> Â I've tried GLdouble, and it doesn't really improve things with the
> current code.
>
> Is there a way to short circuit those realToFrac calls if we know the
> input and output are the same type? Â Is there a way merge the nested
> calls?
You can try rewrite rules
{-# RULES
"realToFrac2/realToFrac" realToFrac . realToFrac = realToFrac
"realToFrac/id" realToFrac = id
#-}
but I'm afraid the second won't work at all, then you'd have to specify all
interesting cases yourself (there are rules for the cases Double -> Double
and Float -> Float in GHC.Float, rules for converting from/to CFloat and
CDouble in Foreign.C.Types, so those should be fine too)
"realToFrac/GLclampf->GLclampf" realToFrac = id :: GLclampf -> GLclampf
and what ese you need.
Whether the first one will help (or even work), I don't know either, you
have to try.
>
> Any other thoughts on what I can do here? Â The slow down between the two
> implementations is at least 20x, which seems like a steep penalty to
> pay.
In case of emergency, put the needed rewrite rules into the source of
OpenGLRaw yourself.
>
> And while I'm at it, is turning on FlexibleInstances the only way to
> create an instance for (a,a)?
Yes. Haskell98 doesn't allow such instance declarations, so you need the
extension.
------------------------------
Message: 4
Date: Wed, 15 Sep 2010 02:32:20 -0700
From: Greg <[email protected]>
Subject: Re: [Haskell-beginners] Padding List with Zeros
To: jean verdier <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
> I'm sorry to have posted incomplete and uncommented code, I should think
> twice next time.
I can hardly complain after having done the same two posts earlier... =)
I botched the same condition, too-- go figure.
On Sep 15, 2010, at 1:54 AM, jean verdier wrote:
> You're right, i didn't compile with -fwarn-incomplete-patterns and it
> bite me.
> You may also write
> g as [] = map (\_ -> 0) as
> or some combination of take length and repeat.
> g as [] = take (length as) (repeat 0)
>
> I'm sorry to have posted incomplete and uncommented code, I should think
> twice next time.
>
>
> On Wed, 2010-09-15 at 01:43 -0700, Greg wrote:
>> I think you also need to handle the case where the second parameter to g is
>> empty (where list_A has non-matching elements after list_B is exhausted).
>>
>> g (a:as) [] = 0 : g as []
>>
>> or something similar in between the two existing definitions?
>>
>>
>> On Sep 15, 2010, at 1:34 AM, jean verdier wrote:
>>
>>>
>>>
>>> list_A = [0,10,20,30,40,50]
>>> list_B = [0,10,50]
>>> list_C = [2,1,-5]
>>>
>>> f a b c = g a (zip b c)
>>>
>>> g [] _ = []
>>> g (a:as) xs@((b,c):xs')
>>> | a > b = error "b is not a subset of a"
>>> | a == b = c : g as xs'
>>> | a < b = 0 : g as xs
>>>
>>> main = do
>>> print (f list_A list_B list_C)
>>>
>>>
>>>
>>> On Wed, 2010-09-15 at 09:28 +0200, Lorenzo Isella wrote:
>>>> Hi Antoine,
>>>> Unfortunately these are really truly lists and not sets (for instance,
>>>> the ordering of elements matter and some of them may be repeated).
>>>>
>>>> Lorenzo
>>>>
>>>> On 09/15/2010 01:55 AM, Antoine Latter wrote:
>>>>> Are these truly lists, or would you be better suited using Sets, Maps or
>>>>> IntMaps?
>>>>>
>>>>> Then you can use some of the unionWith functions to decide what to
>>>>> insert, or you can simply wrap the looking functions to return zero on
>>>>> failure.
>>>>>
>>>>
>>>>
>>>>
>>>>> Antoine
>>>>>
>>>>> On Sep 14, 2010 6:35 PM, "Lorenzo Isella" <[email protected]
>>>>> <mailto:[email protected]>> wrote:
>>>>>> Dear All,
>>>>>> I still have to find my way with immutable lists and list comprehension.
>>>>>> Consider the following lists
>>>>>>
>>>>>> A=[0,10,20,30,40,50]
>>>>>> B=[0,10,50] (i.e. B is a subset of list A; list A is already ordered in
>>>>>> increasing order and so is B).
>>>>>> C=[2,1,-5] i.e. there is a corresponding element in C for every element
>>>>>> in B.
>>>>>>
>>>>>> Now, I would like to define a new list D having length equal to the
>>>>>> length of A. The elements of D in the position of the elements of A in
>>>>>> common with B are equal to the corresponding entries in C, whereas the
>>>>>> other ones are zero i.e.
>>>>>> D=[2,1,0,0,0,-5]. How can I achieve that? The first thought that comes
>>>>>> to my mind is to define a list of zeros which I would modify according
>>>>>> to my needs, but that is not allowed...
>>>>>> Many thanks
>>>>>>
>>>>>> Lorenzo
>>>>>> _______________________________________________
>>>>>> Beginners mailing list
>>>>>> [email protected] <mailto:[email protected]>
>>>>>> http://www.haskell.org/mailman/listinfo/beginners
>>>>>
>>>>
>>>> _______________________________________________
>>>> Beginners mailing list
>>>> [email protected]
>>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
>
------------------------------
Message: 5
Date: Wed, 15 Sep 2010 11:11:41 +0100
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] Padding List with Zeros
To: Lorenzo Isella <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
FWIW here is another solution, using only lists. (Whereas I agree with
others who suggested using *proper* data structures rather than 'lists for
everything')
-- walks over both lists, always consumes an element from
-- the first list, consumes an element from the second list
-- only if values are equal.
ix :: Ord a => [a] -> [a] -> [b -> Maybe b]
ix (a:as) (b:bs) | a == b = Just : ix as bs
ix (a:as) (b:bs) | a < b = const Nothing : ix as (b:bs)
ix _ _ = []
-- consume a value from the second list, only if the current
-- function coming from the first list evaluates to a Just
proj :: [a -> Maybe b] -> [a] -> [Maybe b]
proj [] _ = []
proj (f:fs) (x:xs) =
let fx = f x
nextxs Nothing = x : xs
nextxs _ = xs
in fx : proj fs (nextxs fx)
*Main> proj (ix inp_a inp_b) inp_c
[Just 2,Just 1,Nothing,Nothing,Nothing,Just (-5)]
And of course, if you really want to substitute 0's for Nothings, you can do
it using something like:
to0s :: Num a => [Maybe a] -> [a]
to0s = map (fromMaybe 0)
*Main> to0s $ proj (ix inp_a inp_b) inp_c
[2,1,0,0,0,-5]
Hope this helps,
Ozgur
On 15 September 2010 00:35, Lorenzo Isella <[email protected]> wrote:
> Dear All,
> I still have to find my way with immutable lists and list comprehension.
> Consider the following lists
>
> A=[0,10,20,30,40,50]
> B=[0,10,50] (i.e. B is a subset of list A; list A is already ordered in
> increasing order and so is B).
> C=[2,1,-5] i.e. there is a corresponding element in C for every element in
> B.
>
> Now, I would like to define a new list D having length equal to the length
> of A. The elements of D in the position of the elements of A in common with
> B are equal to the corresponding entries in C, whereas the other ones are
> zero i.e.
> D=[2,1,0,0,0,-5]. How can I achieve that? The first thought that comes to
> my mind is to define a list of zeros which I would modify according to my
> needs, but that is not allowed...
> Many thanks
>
> Lorenzo
> _______________________________________________
> 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/20100915/8bddf651/attachment.html
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 27, Issue 35
*****************************************