Jim Burton jim at sdf-eu.org
Tue May 29 06:05:59 EDT 2007

```
Eduardo01 wrote:
>
> Hi! First, nice to meet you guys.
> My name's Eduardo(or call me Edward, easier for you).
>
Hi Eduardo,

> I'm brazilian and computation science student.
> I'm at first period and the main language i'm learning is Haskell and i
> will learn C 'till the end of this period.
>
Great! Please use the haskell-cafe mailing list for these kind of questions,
this one is used more for announcements and that sort of thing.

> So, as the title suggest, my doubt about haskell is on recursive
> functions, especifically where you have two arguments. Like this:
>
> Build a function that sum all the even numbers between the interval of n
> and k, where n < k.
>
> I was doing this:
>
> somapar :: Int -> Int -> Int
> somapar n k = if even n && even k
>               then somapar (n+1) (k-1) + n + k
>               else if even n && not(even k)
>                    then somapar (n+1) (k-1) + n
>                    else if not (even n) && even k
>                         then somapar (n+1) (k-1) + k
>                         else somapar (n+1) (k-1)
>
> I checked this out and think that is allright, but I don't have the stop
> condition. So when i execute it, the function falls into an infinite loop.
> The other thing is that I think that there are a lot of "ifs" in the
> function, tell me if i could remove some of them.
>

You're right, somapar never returns a value -- at what point do you think it
should? Try to work it out on pencil and paper for some small values. How do
you keep track of what the current sum is? Also, check out some Haskell list
functions, if you're allowed to use them for this ->
You can do away with the "ifs"  with guards like

f x
| x > 0       = 1
| otherwise = 0

>
> Well, that's it. Sorry by the so so english and thank you.
>

--
View this message in context: http://www.nabble.com/Doubt-about-recursive-functions-tf3831290.html#a10851169