English 中文(简体)
Haskell的功能曲线
原标题:Function currying in Haskell

我有以下职能:

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.

如何纠正错误? 感谢。

最佳回答

除电力局外,还使用你的代码。 我不能用<代码>地图(授权100)[2,5]

*Main> map (powerOf 100) [2,5]
[2,2]

Do you get any sort of error?


关于您的第二个问题:

powerOfN :: Int -> Int
powerOfN num = powerOf num

签字类型不正确。

电力局负责进行分类,并收回一个带有愤怒和重新分类的职能。

因此,签字类型应当

powerOfN :: Int -> (Int -> Int)

内容与(Thanks to delnan 。 注

powerOfN :: Int -> Int -> Int
问题回答

我认为,我看到你的混淆。 你们想要一个“强权”的版本,这只是一个论点,因此,你试图界定“强权”一词,这只是一个论点。 但事实上,“力量”已经做到了这一点。 您必须读为“和”类操作员。 如同“+”是数量上的操作者,因此“->”是类型上的操作者;它使用两种类型,并回报一种新的类型。 So

Foo -> Bar

职能从“Foo”到“Bar”。 但是,由于这是一种类型,你可以向它申请另一种“和”行动,如:

Int -> (Int -> Int)

这就意味着要履行“Int”职能,并恢复新的职能,即第二次“Int”,并因此返回“Int”。

Haskell将“->”运营商定义为正确的联系,因此可以删除本案中的方括号,因此,它认为:

Int -> Int -> Int

哪一类是你的“力量”职能。 因此,你可以使用“强权”并给出一个论点,而你回来的是一种新职能,期望有第二个论点。 你们想要的是什么。





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签