
Another thing, while toying, I found out that a comparison (n <= 0) takes three reductions more than (n < 1) according to my hugs, so changing the definition of splitAt thus, we require (3*n) reductions less. But the number of reductions and speed are different things, as witnessed by the above, so would it be an improvement to replace "n <= Integerliteral"-queries by "n < Integerliteral"-queries or doesn't that make any difference at all in execution time?
I don't know about in Hugs, but in (compiled, optimized) GHC it should make no difference. Presumably, if you are running something in an interpreter micro-optimizing isn't worthwhile.
Finally, in several contexts I needed to cons an element to one of a pair of lists, so I defined
infixr 5 &,§
(&) :: a -> ([a],[b]) -> ([a],[b]) x & (xs,ys) = (x:xs,ys)
(§) :: b -> ([a],[b]) -> ([a],[b]) y § (xs,ys) = (xs,y:ys).
Well, to start, the type signatures are unnecessarily restrictive. Then, the function that also is not in the Report, but does come up quite a bit by people who get into a point-free or categorical style is the bifunctor, (***) :: (a -> b) -> (c -> d) -> (a,c) -> (b,d) f *** g = \(a,b) -> (f a,g b) this is an instance of (***) in Control.Arrow, hence the name. So, your first function is, (&) x = (x:) *** id or using another function from Control.Arrow, (&) x = first (x:) I can say that I have wanted (***), I can't say that I've ever wanted your two functions. Also, first (x:) seems to be more self-documenting.

Am Samstag, 15. Januar 2005 14:36 schrieben Sie:
Well, to start, the type signatures are unnecessarily restrictive. Yep, but since I always needed them for taking elements that satisfied either of two predicates from a list, that was the type that first came to mind (actually the zero'th type used ([a],[a]) ). Then, the function that also is not in the Report, but does come up quite a bit by people who get into a point-free or categorical style is the bifunctor, (***) :: (a -> b) -> (c -> d) -> (a,c) -> (b,d) f *** g = \(a,b) -> (f a,g b) this is an instance of (***) in Control.Arrow, hence the name.
That's good to know, thanks.
So, your first function is, (&) x = (x:) *** id or using another function from Control.Arrow, (&) x = first (x:)
I can say that I have wanted (***), I can't say that I've ever wanted your two functions. Also, first (x:) seems to be more self-documenting.
I haven't read Control.Arrow yet, but it seems at first glance that I should write first (x:) (xs,y) second (y:) (x,ys) instead? That looks good to me, more to type, alas, but much better to read. Many thanks, Daniel
participants (2)
-
Daniel Fischer
-
Derek Elkins