我想在Int列表中使用类似square的操作,然后在字符串中给出初始值和结果。例如:Haskell IntList字符串
IntListToString平方[5,6,7]应该导致像5 = 25 6 = 36 = 7 = 49
我的猜测是声明具有看起来像这样
IntListToString :: (Int -> Int) -> [Int] -> String
Im与功能本身挣扎。我必须使用地图功能,然后转换成正确的格式吗?
你对解决这个问题的建议是什么?
我想在Int列表中使用类似square的操作,然后在字符串中给出初始值和结果。例如:Haskell IntList字符串
IntListToString平方[5,6,7]应该导致像5 = 25 6 = 36 = 7 = 49
我的猜测是声明具有看起来像这样
IntListToString :: (Int -> Int) -> [Int] -> String
Im与功能本身挣扎。我必须使用地图功能,然后转换成正确的格式吗?
你对解决这个问题的建议是什么?
是,尽量
showBeforeAfter before after = show before ++ "=" ++ show after
transformBeforeAfter f x = showBeforeAfter x (f x)
mapBeforeAfter f = map (transformBeforeAfter f)
beforeAfter f xs = unwords (mapBeforeAfter f xs)
其中beforeAfter是你IntListToString的实现。请注意,函数名称必须以符号(不包括冒号)或小写字母开头。
感谢您的回答。它完美的作品。在每个结果之后是否有简单的方法来实现新行? – toasttoast
是的。使用unlines而不是unwords。 –
'f g =单词。 map(\ x-> show x ++“=”++ show(g x))'应该可以工作。 对于f(^ 2)[5,6,7]我得到结果“5 = 25 6 = 36 7 = 49” – Antisthenes