'model n of integer type
I am working on a module which involves a set of natural numbers. As a result I need to model some n of type integer. How can I go about it?
Eg. sum of i for continuously increasing sequence of i starting at 1 = n(n+1)/2
How can I model n here?
Solution 1:[1]
The requirements are that we should be able to use n as an integer. I think I figured it out.
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chris |
