Pabl0o0

Untitled

Nov 6th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. type code = Elem of bool | Not | And | Or | Xor;;
  2.  
  3. let eval le =
  4. List. hd (List.fold_left (fun stack e ->
  5. (match e with
  6. | Elem(b) ->
  7. b::stack
  8. | Not ->
  9. if List.length stack < 1 then failwith "Za malo argumentow dla not"
  10. else (not (List.hd stack))::(List.tl stack)
  11. | And ->
  12. if List.length stack < 2 then failwith "Za malo argumentow dla and"
  13. else ((List.hd stack) && (List.hd (List.tl stack)))::(List.tl (List.tl stack))
  14. | Or ->
  15. if List.length stack < 2 then failwith "Za malo argumentow dla or"
  16. else ((List.hd stack) || (List.hd (List.tl stack)))::(List.tl (List.tl stack))
  17. | Xor ->
  18. if List.length stack < 2 then failwith "Za malo argumentow dla xor"
  19. else ((List.hd stack) || (List.hd (List.tl stack)) || (not ((List.hd stack) && (List.hd (List.tl stack))))) ::(List.tl (List.tl stack))
  20. )
  21. ) [] le);;
  22.  
  23. eval [Elem true];;
  24. eval [Elem true; Not];;
  25. eval [Elem true; Not; Elem false; Or; Elem true; Xor; Elem true; And];;
  26. eval [Elem true; And];;
  27. eval [Elem false; Elem true; Not; Elem false; Or; Elem true; Xor; Elem true; And];;
Advertisement
Add Comment
Please, Sign In to add comment