J'ai une fonction:Fonction curryfication dans Haskell
powerOf :: Int -> Int -> Int
Exemple d'utilisation de os:
*Main Data.List> powerOf 100 2
2
*Main Data.List> powerOf 100 5
2
J'ai deux questions. Première - pourquoi cela ne fonctionne pas:
map (powerOf 100) [2, 5]
Je veux obtenir [2, 2].
Et deuxième question. J'essaie de créer une fonction pariatl. Quelque chose comme ceci:
powerOfN :: Int -> Int
powerOfN num = powerOf num
à l'utiliser de telle manière:
let powerOf100 = powerOfN 100
powerOf100 2
powerOf100 5
mais je reçu le message d'erreur:
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
est ici plein de code mai:
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
puissanceDe retour puissance maximale de p en num. Par exemple: 100 = 2 * 2 * 5 * 5, donc powerOf 100 2 = 2. 10 = 2 * 5, donc powerOf 10 2 = 1.
Comment réparer les erreurs? Merci.
Pouvez-vous fournir le code de 'powerOf'? –
'powerOf' semble être cassé ou mal nommé. 100 à la puissance de 2 est 10000 autant que je sache. – delnan
Je poste tout mon code et explique powerOf. – demas