2004-12-28 [長年日記]
λ. 可変長引数の関数?
multi-parameter class を使って可変長引数っぽいことが出来ないかと思って、ちょっと試してみた。引数の型を明示する必要があるのがイヤだな。
class VAFun arg result x | x -> result where collectVAList :: ([arg] -> result) -> [arg] -> x instance VAFun arg Int Int where collectVAList k xs = k (reverse xs) instance VAFun arg result x => VAFun arg result (arg -> x) where collectVAList k xs x = collectVAList k (x:xs) withVAList :: VAFun arg result x => ([arg] -> result) -> x withVAList k = collectVAList k [] vaSum :: VAFun Int Int x => x vaSum = withVAList sum
*Main> vaSum
<interactive>:1:
No instance for (VAFun Int Int x)
arising from use of `vaSum' at <interactive>:1
In the definition of `it': it = vaSum
*Main> vaSum :: Int
0
*Main> vaSum 3 2 :: Int
<interactive>:1:
No instance for (VAFun Int Int (t -> t1 -> Int))
arising from use of `vaSum' at <interactive>:1
When checking the type signature of the expression:
vaSum 3 2 :: Int
In the definition of `it': it = vaSum 3 2 :: Int
*Main> vaSum (3::Int) (2::Int) :: Int
5
[追記] collectVAListではなくwithVAListをVAFunのメソッドにしてしまえる。ytsの日記(2005-01-02) を参照のこと。
[ツッコミを入れる]
