我有以下职能:
powerOf :: Int -> Int -> Int
例如:
*Main Data.List> powerOf 100 2
2
*Main Data.List> powerOf 100 5
2
我有两个问题。 第一,为什么不奏效:
map (powerOf 100) [2, 5]
我想得到[2、2]。
第二个问题。 我试图创造食疗功能。 与此类似:
powerOfN :: Int -> Int
powerOfN num = powerOf num
a. 使用:
let powerOf100 = powerOfN 100
powerOf100 2
powerOf100 5
但是,这只是一个错误信息:
simplifier.hs:31:15:
Couldn t match expected type `Int
against inferred type `Int -> Int
In the expression: powerOf num
In the definition of `powerOfN : powerOfN num = powerOf num
这里完全可以编码:
divided :: Int -> Int -> Bool
divided a b =
let x = fromIntegral a
y = fromIntegral b
in (a == truncate (x / y) * b)
listOfDividers :: Int -> [Int]
listOfDividers num =
let n = fromIntegral num
maxN = truncate (sqrt n)
in [n | n <- [1.. maxN], divided num n]
isItSimple :: Int -> Bool
isItSimple num = length(listOfDividers num) == 1
listOfSimpleDividers :: Int -> [Int]
listOfSimpleDividers num = [n | n <- listOfAllDividers, isItSimple n]
where listOfAllDividers = listOfDividers num
powerOfInner :: Int -> Int -> Int -> Int
powerOfInner num p power
| divided num p = powerOfInner (quot num p) p (power + 1)
| otherwise = power
powerOf :: Int -> Int -> Int
powerOf num p = powerOfInner num p 0
powerOfN :: Int -> Int
powerOfN num = powerOf num
权力 num的最高收益权。 例如,100 = 2 * 2 * 5 *,因此,Of 100 2 = 2. 10 = 2 * 5,因此Of 10 2 = 1.
如何纠正错误? 感谢。