import gleam/should pub type Parser(r) { Pop(Parser(fn(String) -> r)) End(r) } fn apply(parser: Parser(fn(String) -> r), value: String) -> Parser(r) { // fn apply(parser) { case parser { End(func) -> End(func(value)) // Pop(inner) -> apply(inner) Pop(End(func)) -> Pop(End(func(value))) Pop(Pop(End(func))) -> Pop(Pop(End(func(value)))) } } fn decode(parser: Parser(r), input: List(String)) -> Result(r, Nil) { case parser { Pop(inner) -> { // For now just assume there are enough entries in the list case input { [value, ..rest] -> decode(apply(inner, value), rest) [] -> Error(Nil) } } End(value) -> Ok(value) } } pub fn run_test() { let parser = Pop(Pop(Pop(End(fn(a) { fn(b) { fn(c) { tuple(a, b)} }})))) parser == End(tuple("", "")) let tuple("a", "b") = decode(parser, ["a", "b", "c"]) should.equal(3,5) }