I am trying to learn haskell at the moment and I've come across an example problem that I'm having trouble with.
The problem is that imagine we has lists representing numbers, for example the number 12 is [2,1] and 148 is [8,4,1], then how do we add these two lists together as if they were numbers. My intuition is we carry numbers with they add to over 10 similar to how addition is done with large numbers.
My code so far is:
addLnat [x] [y] = rem (x + y) 10 : (quot (x + y) 10) : []
addLnat (x:xs) (y:ys) = (rem (x + y) 10) : w + head (addLnat xs ys)
where w = quot (x + y) 10
However this will not compile and I don't really understand why to me this seems like the solution, For example:
If we start with [3,2,1] and [6,6,9]. We add the 6 and 3 and the quotient is 0 so 9:0+ and repeat until we get to the final case.
Any ideas on why this is not working/compiling?
take 1is a total alternative forhead, anddrop 1fortail, so thatheadOr 0 = sum . take 1andtailOr [] = drop 1. - Will NesstailOr []==drop 1. But nottake 1being in any way equal toheadOr. In any case bothtailOrandheadOrare more general in the possible set of return values. - Shoe