I am trying to learn a bit of Haskell but I am having some problems with understanding how to make programs with pow*.

For example I have to create the following program:

What the program does is 1+2(pow2)+3(pow2)+n(pow2) and then to make sure that n is positive number.

If someone could create such a program that would really help me understand what is going on.

I tried to look in Google Wikipedia Haskell sites but it seems that this language is not used by most programmers and no one really makes such eazy programs.

I don't know of any built in pow function in Haskell (I'm still a novice with it myself), but off the top of my head, you should be able to create a simple pow function like this:

pow :: Int -> Int -> Int
pow x y = x^y

Which you call like this:

pow 3 2

Where 3 is the base value and 2 is the exponent, which should calculate and return the value of 3^2, which is 9!

With regards to the rest of your program, I'm a little confused. The description doesn't make sense.

I'm assuming you need to get the user to input a value for n, which you need to check is positive.
If the value is acceptable, you then calculate the series of values. Looking at the formula you have given, it looks as if it will require a recursive function starting with n^2 and counting down on each recursive call until you get down to 1.

So using the pow function above, we could create a simple recursive function like this:

powthing :: Int -> Int
powthing n
    | n < 1 = 0
    | n == 1 = 1
    | otherwise = pow n 2 + powthing (n-1)

Note: I had no idea what to call this function, so I've simply called it powthing! :)

In the above function, we have a few checks against the value of n. If n<1 we return 0, if n==1 we return 1, otherwise we return the value of n^2 + whatever is returned by a recursive call to the powthing function, passing n-1.

However, as we are always using 2 as the 2nd parameter to the pow function, the pow function itself seems a little redundant. You could just as easily forget the pow function altogether and just plug n^2 into the powthing function instead. e.g.

powthing :: Int -> Int
powthing n
    | n < 1 = 0
    | n == 1 = 1
    | otherwise = n^2 + powthing (n-1)

To understand what happens when the function is called, consider the following:
If we pass a value of 3 to our powthing function e.g.
powthing 3

The following calls are made to the function:
call 1: powthing(3) = 3^2 + powthing(2)
call 2: powthing(2) = 2^2 + powthing(1)
call 3: powthing(1) = 1

After the final recursive call, going back up the callstack:
call 3 returns the value 1.
call 2 returns the value 5 (2^2 + 1)
call 1 returns the value 14 (3^2 + 5)

So the final value returned is 14.

I think that meets your requirement!
I haven't done anything with I/O in Haskell yet, so I don't know anything about getting input from a user. So if that is required for your program, I'm afraid I can't help you!

Also please note that all of the above code is completely off the top of my head. So I haven't tested any of it. But it should be more or less correct!

Comments
+1
Great job!

I can not tell you how greatful I am. THANK YOU! I understand it now.

No probs. Glad to have helped.

As a Haskell newbie myself, I know how frustrating it is when first starting out. Haskell is unlike any other programming language I have ever used. It's taking some time to get used to, but I think it's starting to finally sink in! Heh heh!

One other thing; if this has answered your question, could you please mark as solved? Thanks!

I don't know of any built in pow function in Haskell

Why don't you count ^ as a pow function?

powthing :: Int -> Int

Unless you have a good reason to prefer Int over Integer (like you know for a fact that your use of Integer is causing performance problems in your code and that all your values fit into the Int range), I'd recommend either using Integer or keeping the function polymorphic.

This article has been dead for over six months. Start a new discussion instead.