[Haskell-cafe] Thinking about an unlistN
Benja Fallenstein
benja.fallenstein at gmail.com
Sun Aug 10 17:24:07 EDT 2008
Hi,
On Sun, Aug 10, 2008 at 8:57 PM, Michael Feathers
<mfeathers at mindspring.com> wrote:
> If I have, say, a function f :: a -> a -> a -> a -> b it would be nice to be
> able to just:
>
> unlistN 4 f [1..4]
It indeed doesn't work like this; there's more than one way to do
something *like* this, if you really want to. The closest one is
probably to use type-level numbers:
{-# OPTIONS_GHC -fglasgow-exts #-}
data Zero
data Suc a
zero :: Zero; zero = undefined; suc :: a -> Suc a; suc = undefined
one = suc zero; two = suc one; three = suc two; four = suc three
class Unlist n a b where
type UnlistFn n a b
unlist :: n -> UnlistFn n a b -> [a] -> b
instance Unlist Zero a b where
type UnlistFn Zero a b = b
unlist _ r _ = r
instance Unlist n a b => Unlist (Suc n) a b where
type UnlistFn (Suc n) a b = a -> UnlistFn n a b
unlist _ f (x:xs) = unlist (undefined :: n) (f x) xs
main = print (unlist four (,,,) "abcd")
This prints ('a','b','c','d').
Hope this is fun[*],
- Benja
[*] I hesistate to say "hope this helps" in this case :-)
More information about the Haskell-Cafe
mailing list