假设我已经用 Haskell 类型对自然数进行了编码,并且我有一种方法可以对它们进行加减运算:
data Zero
data Succ n
-- ...
我已经看到了各种创建可变参数函数外观的代码,例如this,它允许以下内容:
buildList "polyvariadic" "function" "wut?" :: [String]
-- ["polyvariadic","function","wut?"]
我想知道的是我是否可以在此基础上构建一个只接受与类型号实例相对应的参数数量的函数。我想要做的看起来像:
one = Succ Zero
two = Succ one
three = Succ two
threeStrings :: String -> String -> String -> [String]
threeStrings = buildList three
threeStrings "asdf" "asdf" "asdf"
-- => ["asdf","asdf","asdf"]
threeStrings "asdf"
-- type checker is all HOLY CHRIST TYPE ERROR
threeStrings "asdf" "asdf" "asdf" "asdf"
-- type checker is all SWEET JESUS WHAT YOU ARE DOING
我知道这很愚蠢,可能是在浪费我的时间,但这似乎是周末有趣的事情。