haskell按模式打印多个变量

haskell按模式打印多个变量,haskell,Haskell,我开始用Haskell写问题的答案,就像这样 problem1 = ... problem2 = ... 为了将它们显示在一起,我一直在使用类似这样的东西 problems x = "print $ zip [1..] [" ++ intercalate "," (take x stringList) ++ "]" where stringList = map ("fromIntegral problem"++) stringInts

我开始用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定义宏,但在将problemi转化为problem1时遇到了麻烦。m4也有中缀运算符反勾号的问题,如

x `mod` 3 == 0
所以我不得不用m4块注释来包装文档的其余部分

我研究了如何定义c预处理器宏,但据我所知,for循环(或循环)是不受支持的


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

你真的需要一次显示所有这些内容吗?这似乎不是一件有用的事,真的

无论如何,您可以稍微更改接口,而不是
(问题1,问题2,…):(积分a)=>a
do
problem::(积分a)=>Int->a

problem 1 = ...
problem 2 = ...
然后收集所有问题是:

problems = zipWith ($) (repeat problem) [1..2]
main = print $ zip [1..] problems

这似乎有点违反直觉,但我用它来比较不同语言的执行速度。它帮助我了解什么语言最快,就像我知道如何用这种语言编码和优化一样