Advertisement
Guest User

Untitled

a guest
Nov 14th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (* 10th function *)
  2. fun traverse f_c f_v f_acc acc exp =
  3.     case exp of
  4.         Constant(x) => f_acc (f_c x, acc)
  5.         | Variable(x) => f_acc (f_v x, acc)
  6.         | Pair([x1,x2]) => f_acc (traverse f_c f_v f_acc acc x1, traverse f_c f_v f_acc acc x2)
  7.         | Operator(_,exp1) => traverse f_c f_v f_acc acc exp1
  8.         | List(head::tail) => f_acc (traverse f_c f_v f_acc acc head, traverse f_c f_v f_acc acc (List(tail)))
  9.         | _ => acc
  10.  
  11. (* 7th function *)
  12. fun count_constants exp = traverse (fn x=>1) (fn x=>0) op+ 0 exp
  13.  
  14. (* 8th function *)
  15. fun sum_constants exp = traverse (fn x=>x) (fn x=>0) op+ 0 exp
  16.  
  17. (* 9th function *)
  18. fun all_variables exp = traverse (fn x=>[]) (fn x=>[x]) (fn (x,acc) => (List.filter (fn y=>null x orelse y<>hd x) acc) @ x) [] exp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement