开发者

Function currying in Haskell

I have a function:

powerOf :: Int -> Int -> Int

example os usage:

*Main Data.List> powerOf 100 2
2
*Main Data.List> powerOf 100 5
2

I have two questions. First - why it doesn't works:

map (powerOf 100) [2, 5]

I want to get [2, 2].

And second question. I trying to create pariatl function. Something like this:

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

to use it such way:

let powerOf100 = powerOfN 100
powerOf100 2
powerOf100 5

but i got the error message:

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

Here is full of may code:

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 = fromInte开发者_如何转开发gral 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

powerOf return maximum power of p in num. For example: 100 = 2 * 2 * 5 *5, so powerOf 100 2 = 2. 10 = 2 * 5, so powerOf 10 2 = 1.

How to fix errors? Thanks.


Using your code, apart from the powerOfN function. I cannot reproduce your problem with map (powerOf 100) [2,5].

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

Do you get any sort of error?


Regarding your second problem:

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

The type signature is incorrect.

powerOfN takes an integer and returns a function that takes an integer and returns an integer.

So the type signature should be

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

Which is the same as (Thanks to delnan for confirming it):

powerOfN :: Int -> Int -> Int


I think I see your confusion. You want a version of "powerOf" that takes a single argument, so you tried to define "powerOfN" which only takes one argument. But in fact "powerOf" already does that. You have to read "->" as a type operator. Just as "+" is an operator on numbers, so "->" is an operator on types; it takes two types and returns a new one. So

Foo -> Bar

is a function from a "Foo" to a "Bar". But since this is a type, you can apply another "->" operation to it, like this:

Int -> (Int -> Int)

which means a function that takes an Int, and returns a new function that takes a second Int and returns an Int as the result.

Haskell defines the "->" operator to be right associative, so the brackets in this case can be dropped, so it looks like this:

Int -> Int -> Int

Which is the type of your "powerOf" function. So you can use "powerOf" and give it one argument, and what you get back is a new function that expects the second argument. Which is what you wanted.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜