daily pastebin goal
60%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type Result =
  2.     | Result of int
  3.     | Error
  4.  
  5. let range modulo x : Result =
  6.     match x with
  7.     | a when a < 0 -> Error
  8.     | a when a >= modulo -> Error
  9.     | a -> Result a
  10.  
  11. let rec join1 modulo (x:Result) rest : Result =
  12.     match x with
  13.     | Error -> Error
  14.     | Result a ->
  15.         match rest with
  16.         | [] -> x
  17.         | y::xy -> match range modulo y with
  18.                    | Error -> Error
  19.                    | Result b -> join1 modulo (Result (a * modulo + b)) xy
  20.  
  21. let join modulo list : Result =
  22.     match modulo with
  23.     | m when m < 2 -> Error
  24.     | m -> match list with
  25.            | [] -> Result 0
  26.            | [x] -> (range m x)
  27.            | x::xs -> join1 modulo (range m x) xs
  28.  
  29. let expect a b =
  30.     printf "Value '%A', expected '%A' => %A\n" a b (a = b)
  31.  
  32. [<EntryPoint>]
  33. let main argv =
  34.     join 10 [] |>expect<| Result 0
  35.     join 10 [1] |>expect<| Result 1
  36.     join 10 [1; 2] |>expect<| Result 12
  37.     join 10 [1; 2; 3] |>expect<| Result 123
  38.     join 10 [1; 2; 3; 4] |>expect<| Result 1234
  39.     join 10 [-1; 2; 3; 4] |>expect<| Error
  40.     join 10 [1; 2; 30; 4] |>expect<| Error    
  41.     0
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top