我正在研究一个涉及一组自然的模块
数字。因此,我需要对一些整数类型的 n 进行建模。我怎样才能
去吧?
例如。 i 的总和,用于从 1 开始连续增加的 i 序列 =
n(n+1)/2
我如何在这里建模 n?
最佳答案
要求是我们应该能够将 n 用作整数。我想我想通了。
type element_i = N of nativeint | CNN of nativeint * nativeint
(* element_i can be an integer or a*n+b represented as (a,b))
let to_string_i e =
match e with
| N z -> "%d" z
| CNN c -> " (%d xn + %d) " (fst c) (snd c)
let plus_i a b =
match (a, b) with
| (N a1, N b1) -> N (a1 + b1)
| (N a1, CNN b1) -> CNN (fst b1, (snd b1) + a1)
| (CNN a1, N b1) -> CNN (fst a1, (snd a1) + b1)
let times_i a b =
match (a, b) with
| (N a1, N b1) -> N (a1 * b1)
| (N a1,CNN b1) -> CNN ((fst b1) * a1, (snd b1) * a1)
| (CNN a1,N b1) -> CNN ((fst a1) * b1, (snd a1) * b1)
关于ocaml - 整数类型的模型 n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4031950/