daily pastebin goal
46%
SHARE
TWEET

Untitled

a guest Oct 12th, 2017 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type (_, _) term =
  2.   | Int: (int -> 'a, 'a) term
  3.   | String: (string -> 'a, 'a) term
  4.   | Const: string -> ('a, 'a) term
  5.  
  6. type (_, _) term_list =
  7.   | []   : ('a, 'a) term_list
  8.   | (::) : ('a, 'b) term * ('b, 'c) term_list -> ('a, 'c) term_list
  9.  
  10. (* Construct a function that takes same arguments and produces a string *)
  11. let rec encoder: type b. (b, string) term_list -> string -> b = function
  12.   | Int :: xs ->
  13.     fun acc v -> encoder xs (Printf.sprintf "%s %d" acc v)
  14.   | String :: xs ->
  15.     fun acc v -> encoder xs (Printf.sprintf "%s '%s'" acc v)
  16.   | Const s :: xs  ->
  17.     fun acc -> encoder xs (Printf.sprintf "%s %s" acc s)
  18.   | [] -> fun x -> x
  19.  
  20. let expr = Const "SELECT a FROM x WHERE a =" :: Int :: Const "AND x.y =" :: String :: Const "ORDER BY a" :: []
  21. let expr_func = encoder expr ""
  22.    
  23. let () = Printf.printf "Result: %s\n" (expr_func 5 "value")
RAW Paste Data
Top