Last active
August 29, 2015 14:00
-
-
Save OnorioCatenacci/420f3ae9f5bb6d5f8f99 to your computer and use it in GitHub Desktop.
A Naive Implementation Of A Stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Stack do | |
defstruct name: "", value: 0 | |
def init() do | |
_s = [] | |
end | |
def push(name, value, s) do | |
s_new = [%Stack{name: name, value: value}, s] | |
{:ok,s_new} | |
end | |
def pop(s) do | |
[h|tail] = s | |
{:ok, h, tail} | |
end | |
def depth(s) do | |
length(s) | |
end | |
end | |
#Use: | |
# s = Stack.init() | |
# {:ok, s} = Stack.push("a",1,s) | |
# {:ok, item, s} = Stack.pop(s) | |
# l = Stack.depth(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment