Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Result =
- | Result of int
- | Error
- let range modulo x : Result =
- match x with
- | a when a < 0 -> Error
- | a when a >= modulo -> Error
- | a -> Result a
- let rec join1 modulo (x:Result) rest : Result =
- match x with
- | Error -> Error
- | Result a ->
- match rest with
- | [] -> x
- | y::xy -> match range modulo y with
- | Error -> Error
- | Result b -> join1 modulo (Result (a * modulo + b)) xy
- let join modulo list : Result =
- match modulo with
- | m when m < 2 -> Error
- | m -> match list with
- | [] -> Result 0
- | [x] -> (range m x)
- | x::xs -> join1 modulo (range m x) xs
- let expect a b =
- printf "Value '%A', expected '%A' => %A\n" a b (a = b)
- [<EntryPoint>]
- let main argv =
- join 10 [] |>expect<| Result 0
- join 10 [1] |>expect<| Result 1
- join 10 [1; 2] |>expect<| Result 12
- join 10 [1; 2; 3] |>expect<| Result 123
- join 10 [1; 2; 3; 4] |>expect<| Result 1234
- join 10 [-1; 2; 3; 4] |>expect<| Error
- join 10 [1; 2; 30; 4] |>expect<| Error
- 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement