Elixir Bracket
defmodule Bracket do
def bracket(s) do
result =
s
|> String.graphemes()
|> Enum.reduce_while([], fn ch, stack ->
case ch do
")" -> pop_match(stack, "(")
"]" -> pop_match(stack, "[")
"}" -> pop_match(stack, "{")
_ -> {:cont, [ch | stack]}
end
end)
if result == [], do: 1, else: 0
end
defp pop_match([], _expected), do: {:halt, :mismatch}
defp pop_match([top | rest], expected) do
if top == expected, do: {:cont, rest}, else: {:halt, :mismatch}
end
end
This uses a simple stack approach: open brackets go in, matching closing brackets pop them out.