Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type pure =
- | Binary of expression * Ops.t * expression
- | Atom of int
- and bracketed = pure
- and expression =
- | Bracketed of bracketed
- | Pure of pure
- ;;
- type pure = Binary of expression * Ops.t * expression | Atom of int
- and bracketed = pure
- and expression = Bracketed of bracketed | Pure of pure
- let rec enum' i =
- if i = 0 then [(Atom 0)]
- else (
- let children = enum (i - 1) in
- List.cartesian_product children children |> List.map ~f:(fun (child1, child2) ->
- List.concat_map Ops.all ~f:(fun op ->
- [(Binary (child1, op, child2))]
- ))) |> List.concat
- and enum i = let children = enum' i in
- [List.map children ~f:(fun child -> Pure(child));
- List.map children ~f:(fun child -> Bracketed(child))
- ] |> List.concat
- ;;
- let ops_to_str = function
- | Ops.Plus -> "+"
- | Minus -> "-"
- | Mult -> "*"
- | Div -> "/"
- ;;
- let rec pure_to_str = function
- | Binary (e1, op, e2) -> (expr_to_str e1) ^ (ops_to_str op) ^ (expr_to_str e2)
- | Atom x -> Int.to_string x
- and bracket_to_str e = "(" ^ (pure_to_str e) ^ ")"
- and expr_to_str = function
- | Bracketed x-> bracket_to_str x
- | Pure x -> pure_to_str x
- ;;
- enum 2 |> List.map ~f:expr_to_str;;
- - : string list =
- ["0+0+0+0"; "0+0-0+0"; "0+0*0+0"; "0+0/0+0"; "0+0+0-0"; "0+0-0-0"; "0+0*0-0";
- "0+0/0-0"; "0+0+0*0"; "0+0-0*0"; "0+0*0*0"; "0+0/0*0"; "0+0+0/0"; "0+0-0/0";
- "0+0*0/0"; "0+0/0/0"; "0+0+0+(0)"; "0+0-0+(0)"; "0+0*0+(0)"; "0+0/0+(0)"; ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement