函数bigAdd(两个int列表的加法) [英] Function bigAdd (addition of two int lists)
问题描述
我需要有关bigAdd的帮助,例如我应该为f base和arg确切放置什么.大加法应该采用2个int数组,然后将和输出到另一个int数组中,例如
I need help with the bigAdd, like what exactly I should put for the f base and arg. Big add is supposed take in 2 int arrays and output the sum into another int array like
# bigAdd [9;9] [1;0;0;2];;
- : int list = [1;1;0;1]
# bigAdd [9;9;9;9] [9;9;9];;
- : int list = [1;0;9;9;8]
我到目前为止
let rec padZero l1 l2 =
if List.length l1 > List.length l2 then (padZero l1 ([0]@l2))
else if List.length l2 > List.length l1 then (padZero ([0]@l1) l2)
else (l1, l2)
let rec removeZero l = match l with
|[]->
|h::t-> if h == 0 then removeZero t else l
let bigAdd l1 l2 =
let add (l1, l2) =
let f a x = failwith "to be implemented" in
let base = failwith "to be implemented" in
let args = failwith "to be implemented" in
let (_, res) = List.fold_left f base args in
res
in
removeZero (add (padZero l1 l2))
现在我有
let bigAdd l1 l2 =
let add (l1, l2) =
let f a x = failwith "to be implemented" in
let base = 0 in
let args = List.combine l1 l2 in
let (_, res) = List.fold_left f base args in
res
in
removeZero (add (padZero l1 l2)
我很确定args应该是应该的,但是基数可能是错误的,我也不知道如何写f.列表中添加的部分到底从哪里来?我要先将每个in列表转换为int,然后添加它们,然后将其转换回int列表,还是直接添加它们,如果是,如何以及在骨架中的位置.
I'm pretty sure the args is what it's supposed to be, but the base is probably wrong and I have no idea how to write the f. Where exactly does the addition part of the lists come from in this skeleton? Do I convert each in list into an int first and then add them and convert it back to an int list or add them directly and if so how and where in the skeleton.
有人可以向我描述类型f,base和args以及它们的功能是什么?我很混乱.
Can someone describe to me the type f, base, and args will be and what their functions should be? I'm so confused.
推荐答案
问题的重点是了解函数f a x
以及变量base
和args
应该是什么.这可能不是您第一次接触List.fold_left
,所以我建议您回顾一下以前使用List.fold_left
进行的练习,并向您提出有关上述变量的想法.如果您无法提供任何代码,请用英语解释您对它们的理解.
The point of the question is to understand what the function f a x
and the variables base
and args
should be. This probably isn't your first encounter with List.fold_left
, so I'd suggest to review previous exercises with List.fold_left
, and add to your question your ideas about the above variables. If you can't come up with any code, explain in English what you understand about them.
您需要先了解f
.完成后,您将清楚地看到f
需要什么作为base
和args
.
要理解f
,请考虑如何手动进行逐位加法.
You need to understand f
first. Once done, you will see clearly what f
needs as base
and args
.
To understand f
, think about how you would do an addition by hand, digit by digit.
您绝对不会将列表转换为int,bigAdd
的全部要点是处理比最大int
大的整数.
You definitely do not convert the lists to int, the whole point of bigAdd
is to handle huge integers, way larger than the maximum int
.
关于辅助功能的侧面说明:
Side note about the auxilliary functions:
-
removeZero
很好 -
padZero
多次调用List.length
,这很慢.相反,您应该计算要添加的零的数量,然后在不调用List.length
的情况下将它们全部添加. - 用
0 :: l1
替换[0]@l1
.
removeZero
is goodpadZero
is callingList.length
several times, which is slow. You should instead compute the number of zeros to add, then add them all without callingList.length
.- replace
[0]@l1
with0 :: l1
.
这篇关于函数bigAdd(两个int列表的加法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!