Skip to content

Instantly share code, notes, and snippets.

@OnorioCatenacci
Last active August 29, 2015 14:00
Show Gist options
  • Save OnorioCatenacci/420f3ae9f5bb6d5f8f99 to your computer and use it in GitHub Desktop.
Save OnorioCatenacci/420f3ae9f5bb6d5f8f99 to your computer and use it in GitHub Desktop.
A Naive Implementation Of A Stack
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