sum problem

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

sum problem

Roelof Wobben
Hello,

The exercises of CIS 194 are really hard.

Now I have to sum all the numbers in a array with a catch
If a number is greater then 10 it has to be dived in single numbers.

so [ 1 , 12, 3] will be [ 1, 1,2,3] and then sum up.

So far I have this :

-- | sum all the digits of a array
sumDigits :: [Integer] -> Integer
sumDigits n
    | n == 0 = acc
    | n < 10 = acc + n
    | otherwise = sumDigits acc + n `mod` 10 + n `rem` 10


But now I see a message that acc is not known which is not wierd.

But when I do sumDigits n acc I see a message that sumDigits has only 1
parameter where I give it to,

When I only had to sum up i is easy , I could use map [+] xs

Anyone  who can give a me tip without giving the answer otherwise I do
not learn anything.

Roelof


Reply | Threaded
Open this post in threaded view
|

sum problem

yi lu
Do not try to `sum` all at first. See how you can deal with one integer.

On Sat, Feb 7, 2015 at 4:53 PM, Roelof Wobben <r.wobben at home.nl> wrote:

> Hello,
>
> The exercises of CIS 194 are really hard.
>
> Now I have to sum all the numbers in a array with a catch
> If a number is greater then 10 it has to be dived in single numbers.
>
> so [ 1 , 12, 3] will be [ 1, 1,2,3] and then sum up.
>
> So far I have this :
>
> -- | sum all the digits of a array
> sumDigits :: [Integer] -> Integer
> sumDigits n
>    | n == 0 = acc
>    | n < 10 = acc + n
>    | otherwise = sumDigits acc + n `mod` 10 + n `rem` 10
>
>
> But now I see a message that acc is not known which is not wierd.
>
> But when I do sumDigits n acc I see a message that sumDigits has only 1
> parameter where I give it to,
>
> When I only had to sum up i is easy , I could use map [+] xs
>
> Anyone  who can give a me tip without giving the answer otherwise I do not
> learn anything.
>
> Roelof
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20150207/003d8fe5/attachment.html>

Reply | Threaded
Open this post in threaded view
|

sum problem

Roelof Wobben
Reply | Threaded
Open this post in threaded view
|

sum problem

Joel Neely
In reply to this post by Roelof Wobben
Question/hint:  You refer to acc three times, all in the right-hand-side
expressions. Where is acc defined?

-jn-

On Sat, Feb 7, 2015 at 2:53 AM, Roelof Wobben <r.wobben at home.nl> wrote:

> Hello,
>
> The exercises of CIS 194 are really hard.
>
> Now I have to sum all the numbers in a array with a catch
> If a number is greater then 10 it has to be dived in single numbers.
>
> so [ 1 , 12, 3] will be [ 1, 1,2,3] and then sum up.
>
> So far I have this :
>
> -- | sum all the digits of a array
> sumDigits :: [Integer] -> Integer
> sumDigits n
>    | n == 0 = acc
>    | n < 10 = acc + n
>    | otherwise = sumDigits acc + n `mod` 10 + n `rem` 10
>
>
> But now I see a message that acc is not known which is not wierd.
>
> But when I do sumDigits n acc I see a message that sumDigits has only 1
> parameter where I give it to,
>
> When I only had to sum up i is easy , I could use map [+] xs
>
> Anyone  who can give a me tip without giving the answer otherwise I do not
> learn anything.
>
> Roelof
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



--
Beauty of style and harmony and grace and good rhythm depend on simplicity.
- Plato
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20150207/c5153f11/attachment.html>

Reply | Threaded
Open this post in threaded view
|

sum problem

Roelof Wobben