How to use IntInf or LargeInt in SML?

我只是一个虾纸丫 提交于 2020-01-04 05:07:34

问题


I want to to perform computations with large integers in SML, through functions like pow in this link:

http://www.standardml.org/Basis/int-inf.html#IntInf:STR:SPEC

But how do I get to use this "library"?


UPDATE:

Thanks for the answer. I got it. I also had to change the limit for printing with

Control.Print.intinfDepth := 10000;

I made my own pow function for IntInfs (and it works) like this:

fun power 0 = IntInf.toLarge 1
  | power n = IntInf.toLarge 2 * power(n-1);

回答1:


It depends on which implementation you use, but generally you need to convert your Int's to LageInt/InfInf types with the Int.toLarge:

(* will be types as an IntInf *)
val aa = 10983298432984329843298432984329843298432987987987432987987987432987
val a = IntInf.pow(aa,10);

(* explicit type as if some other constraint had enforced this*)
val b = 10 : int
val c = Int.toLarge b;

val d = IntInf.pow(c, b);

The variable aa may not be parsed in your interpreter. It depends on what you use. I have tested it in poly and mlton.

where the above gets the types (given by mlton with -show-basis flag):

val a: intInf
val aa: intInf
val b: int32
val c: intInf
val d: intInf



回答2:


-fun prod (a: IntInf.int, b:IntInf.int) : IntInf.int = a+b;

val prod = fn : IntInf.int * IntInf.int -> IntInf.int

for example

-> on how to represent a big integer : IntInf.int as a parameter inside a function



来源:https://stackoverflow.com/questions/7807441/how-to-use-intinf-or-largeint-in-sml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!