Guest User

Immutable "stack"

a guest
Apr 6th, 2016
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.39 KB | None | 0 0
  1. //Immutable "stack"
  2.  
  3. module Stack =
  4.  
  5.     let private emptyError () = failwith "empty"
  6.  
  7.     let push e list = e :: list
  8.    
  9.     let pop = function
  10.     | _ :: xs -> xs
  11.     | _ -> emptyError()
  12.    
  13.     let top = function
  14.     | x :: _ -> x
  15.     | _ -> emptyError()
  16.    
  17.  
  18. []
  19. |> Stack.push "x"
  20. |> Stack.push "y"
  21. |> Stack.push "z"
  22. |> Stack.pop
  23. |> Stack.top
  24. |> printfn "top value: %s"
Advertisement
Add Comment
Please, Sign In to add comment