最简单的可变参数函数

罗布兹

我怎样才能做到这一点

assert toList 1 2 3 == [1, 2, 3]

我在看

https://www.haskell.org/haskellwiki/Varargs

http://chris-taylor.github.io/blog/2013/03/01/how-haskell-printf-works/

http://gotoanswer.stanford.edu/how_to_write_a_haskell_function_that_takes_a_variadic_function_as_an_argument-9197054/

http://paczesiowa.blogspot.de/2010/03/polyvariadic-primefib-problem.html

但是我还没有掌握它(对Haskell初级的学习还很多)

到目前为止我的尝试

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE OverlappingInstances #-}

class ToVector r where
    process :: Int -> r

instance ToVector Int where
    process = id

instance ToVector r => ToVector (Int -> r) where
    process s = \v -> process v

对此表示欢迎

*Main> process 1 2 3

<interactive>:158:1:
Could not deduce (ToVector (a0 -> a1 -> t))
  arising from the ambiguity check for ‘it’
from the context (ToVector (a -> a2 -> t), Num a2, Num a)
  bound by the inferred type for ‘it’:
             (ToVector (a -> a2 -> t), Num a2, Num a) => t
  at <interactive>:158:1-13
The type variables ‘a0’, ‘a1’ are ambiguous
When checking that ‘it’
  has the inferred type ‘forall a a1 t.
                         (ToVector (a -> a1 -> t), Num a1, Num a) =>
                         t’
Probable cause: the inferred type is ambiguous
*Main> 

非常欢迎任何帮助和解释

克罗基亚

这里的示例演示了如何使用您的代码。问题(由@Cubic正确识别)是多态性。特别是,您可以在示例代码中看到使用类型调用方法

main :: IO ()
main = do printAll 5 "Mary" "had" "a" "little" "lamb" 4.2
          printAll 4 3 5

您需要执行以下操作:

foo :: Int
foo = process (1 :: Int) (2 :: Int) (3 :: Int)

这指定的类型process,即to Int -> Int -> Int -> Int您还需要指定数字的类型(与上面的示例不同),因为您仅为Ints创建了一个实例,而它1可以是任何Num类型。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章