0

我开始像这样在 Haskell 中写项目欧拉问题的答案

problem1 = ...

problem2 = ...

一起显示它们我一直在使用这样的东西

problems x = "print $ zip [1..] [" ++ intercalate "," (take x stringList) ++ "]"
             where
               stringList = map ("fromIntegral problem"++) stringInts
               stringInts = map show [1..]

main = print $ zip [1..] [fromIntegral problem1,fromIntegral problem2,...] (copy-pasta from ghci'ing problems string)

有没有办法在不复制粘贴的情况下做到这一点?

我研究了用 m4 定义宏,但我无法将问题转换为问题 1。m4 也有中缀运算符反引号的问题,例如

x `mod` 3 == 0

所以我不得不将整个文档的其余部分包装在 m4 块注释中

我研究了定义 c 预处理器宏,但据我所知,不支持 for 循环(或根本不支持循环)

我希望有一种 Haskell 方法可以做到这一点

4

1 回答 1

2

你真的需要一次显示它们吗?这似乎不是一件有用的事情,真的。

无论如何,您可以稍微更改界面,而不是(problem1, problem2, ...) :: (Integral a) => ado problem :: (Integral a) => Int -> a

problem 1 = ...
problem 2 = ...

然后收集所有问题是:

problems = zipWith ($) (repeat problem) [1..2]
main = print $ zip [1..] problems
于 2013-03-06T00:31:19.017 回答