我可以将 n 个值的列表应用于采用 n 个值(其中 n 不同)的函数吗?
第一个天真的尝试如下,但编译器(相当)提示 weird self-referential type for applyN
applyN f xs =
case xs of
[] -> f
(x::xs) -> applyN (f x) xs
我看不出折叠是如何工作的,也看不出它的类型签名。
对于上下文,我想获取 N 个 Json 解码器的列表并进行评估
Json.objectN ConstructorN n1 n2 ... nN
显然,如果 n 已知(假设为 2),那么我们有
case lst of
(d1 :: d2 :: _) -> Json.object2 Constructor2 d1 d2
otherwise -> ....
但是如果我不能概括 n,那么需要编写大量代码。
我担心这是不可能的,就像在 Haskell 中那样 needs some special compiler flags .
最佳答案
不,你不能这样做,至少不能没有依赖类型或至少有一些类型级别的欺骗,而 Elm 中没有这些(引用: How do I define Lisp’s apply in Haskell? )
(顺便说一句,这就是为什么有所有 objectN
函数。)
尝试重构您的代码 - f
不能只获取一个列表吗?
关于elm - 将值列表作为函数的参数应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34138447/