Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Stacks
- let private stackEmptyError () = failwith "Stack empty"
- //1.) Klasická OOP implementácia zásobníka
- type MutableStack(?stack : MutableStack) =
- let mutable list =
- match stack with
- | Some stack -> stack.List
- | _ -> []
- member internal s.List = list
- member s.Push(value) = list <- value :: list
- member s.Pop() =
- let h, t =
- match list with
- | h :: t -> h, t
- | _ -> stackEmptyError()
- list <- t
- h
- member s.Top() =
- match list with
- | h :: _ -> h
- | _ -> stackEmptyError()
- // Použitie
- let stack = new MutableStack()
- stack.Push "aaa"
- stack.Push "bbb"
- stack.Push "ccc"
- printfn "hodnota je %s" (stack.Pop()) //Vypíše "ccc"
- (****************************************************************************************************)
- //2.) Funkcionálna implementácia zásobníka
- module Stack =
- let push e list = e :: list
- let pop = function
- | _ :: t -> t
- | _ -> stackEmptyError()
- let top = function
- | h :: _ -> h
- | _ -> stackEmptyError()
- // Použitie
- []
- |> Stack.push "aaa"
- |> Stack.push "bbb"
- |> Stack.push "ccc"
- |> Stack.pop |> Stack.top
- |> printfn "hodnota je %s" //Vypíše "bbb"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement