problem with an algorithm
Hallo
this is my first threat in this newsgroup and I'm a newbe about caml so I
need some usefull help with a problem I have.
I have two lists , f.e.: List1 = [1;2;3;4] and List2 = [5;4]. And I will
write a program that can take subtraction in the lists.
example: [1;2;3;4] - [5;4] = [1;1;8;0]
( 1234 - 54 = 1180 )
I have already a program that subtract 2 lists but the other way round.
example: [1;2;3;4] - [5;4] = [6;7;2;4]
( 4321 - 45 = 4276 )
this code do this:
let carry x = if x < 0 then 1 else 0
and sub x = if x < 0 then 10 + x else x;;
let rec Subtraktion = function
([] , [] , c)-> if c = 0 then [] else [c]
| (a::ta, [] , c)-> sub (a-c) ::Subtraktion(ta, [], carry(a-c))
| ([] , b::tb, c)-> sub (b-c) ::Subtraktion([], tb, carry(b-c))
| (a::ta, b::tb, c)-> sub (a-b-c)::Subtraktion(ta, tb, carry(a-b-c));;
How I wrote, I need a program that substract the real number [1;2;3;4] =
1234 and [5;4] = 54
But I can't use functions that just turn over the two lists! If I can use
this function so it will me very easy.
But I can't and so I spent hours with no idea and I need some help.
--
Sorry for my english ;)
So Long, Erwin
Reply to: