Suppose I have a function with the following type signature:
g :: a -> a -> a -> b
I also have a list of a
s—let's call it xs
—that I know will contain at least three items. I'd like to apply g
to the first three items of xs
. I know I could define a combinator like the following:
($$$) :: (a -> a -> a -> b) -> [a] -> b
f $$$ (x:y:z:_) = f x y z
Then I could just use g $$$ xs
. This makes $$$
a bit like uncurry
, but for a function with three arguments of the same type and a list instead of a tuple.
Is there a way to do this idiomatically using standard combinators? Or rather, what's the most idiomatic way to do this in Haskell? I thought trying pointfree
on a non-infix version of $$$
might give me some idea of where to start, but the output was an abomination with 10 flip
s, a handful of head
s and tail
s and ap
s, and 28 parentheses.
(NB: I know this isn't a terribly Haskelly thing to do in the first place, but I've come across a couple of situations where it seems like a reasonable solution, especially when using Parsec. I'll certainly accept "don't ever do this in real code" if that's the best answer, but I'd prefer to see some clever trick involving the ((->) r)
monad or whatever.)
Or rather, what's the most idiomatic way to do this in Haskell?
Idiomatic? If you really want a function that does what ($$$)
does, the code you have is probably as idiomatic as you'll get.
I'd prefer to see some clever trick
Oh, well, in that case.
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE UndecidableInstances #-}
class ListApply f a r | f -> a r where
($...) :: f -> [a] -> r
instance (TypeCast b r) => ListApply b a r where
x $... _ = typeCast x
instance (ListApply f a r) => ListApply (a -> f) a r where
f $... (x:xs) = (f x) $... xs
There you go, a fully general solution: Given a function of arbitrary arity with a signature like a -> a ... -> b
, apply it to as many elements of a list [a]
as necessary. A demonstration:
ones :: [Int]
ones = repeat 1
test1 x = x
test2 x y = x + y
test3 x y z = (x + z) * (y + z)
In GHCi:
> test1 $... ones
1
> test2 $... ones
2
> test3 $... ones
4
I'll certainly accept "don't ever do this in real code" if that's the best answer
You probably want to go with that.
Oh, and a bit of boilerplate needed to run the above code:
class TypeCast a b | a -> b, b->a where typeCast :: a -> b
class TypeCast' t a b | t a -> b, t b -> a where typeCast' :: t->a->b
class TypeCast'' t a b | t a -> b, t b -> a where typeCast'' :: t->a->b
instance TypeCast' () a b => TypeCast a b where typeCast x = typeCast' () x
instance TypeCast'' t a b => TypeCast' t a b where typeCast' = typeCast''
instance TypeCast'' () a a where typeCast'' _ x = x
This is the swiss army knife of type-level metaprogramming, courtesy of Oleg Kiselyov.
f $$$ (x:y:z:_) = f x y z
In my opinion this is the most idiomatic and concise way. If the number of arguments is varying, you can use Template Haskell or do it iteratively - define:
zero = const
next n f (x:xs) = n (f x) xs
then your function is next (next (next zero)))
, and this works for any nesting of next
.
Also you can break it to more primitive combinators:
firstThree (x:y:z:_) = (x,y,z)
uncurry3 f (x,y,z) = f x y z
g f = uncurry3 f . firstThree
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With