我必须这样定义lowestPower:
lowestPower :: Integer -> (Integer,Integer)lowestPower n是(k, m)对,其中m是2的最低幂,其十进制表示以数字n开始,k是它的指数(即2^k = m)。例如,
lowestPower 3 == (5,32)
lowestPower 7 == (46,70368744177664)
fst (lowestPower 982) == 3973我已经尝试过了:
lowestPower :: Integer -> (Integer,Integer)
lowestPower n = (k,m)
where
k <- [0..]
(head . cifras) m = n
2^k = m
cifras :: Integer -> [Integer]
cifras n = [read[d] | d <- show n]发布于 2018-02-22 21:14:21
k <- [0..]不是可以在where块中编写的东西。where块只是定义东西,可能依赖于来自封闭作用域的参数,但它们不能处理这种“循环构造”。您也不能在其中使用等式作为条件条件,例如(head . cifras) m = n。这两者都更像逻辑编程,但Haskell是函数式的,即它不解决命题,而只是简单地赋值函数方程。即所有=行的格式必须为
newVariableOrFunction 〘..arguments..〙 = some expression that can be computed通过使用列表理解,你可以写一些看起来与你的原始代码非常相似的东西,也就是说,你基本上可以对所有可能的值进行显式量化。喜欢
lowestPower n = head
[ (k,m)
| k <- [0..]
, let m = 2^k
, (head . cifras) m == n
]因为列表理解基本上只是一种实现循环方式,所以更自然的做法是只使用递归来定义函数。
https://stackoverflow.com/questions/48927396
复制相似问题