For the assignment, we had to implement something like a very simple sexp parser, for example, for input:
"((a b) ((c d) e) f)"
He will return:
[["a", "b"], [["c", "d"], "e"], "f"]
Since this was part of a larger purpose, only valid input (corresponding to parens & c) is provided to the parser. I came up with the following solution in Ruby:
def parse s, start, stop
tokens = s.scan(/#{Regexp.escape(start)}|#{Regexp.escape(stop)}|\w+/)
stack = [[]]
tokens.each do |tok|
case tok
when start
stack << []
when stop
stack[-2] << stack.pop
else
stack[-1] << tok
end
end
return stack[-1][-1]
end
This may not be the best solution, but it does the job.
Now I am interested in the Haskell idiomatic solution for the main functionality (that is, I donāt care about lexing or the choice of delimiters, if you take the lexical input already it will be fine), if possible, using only the haskell core, without extensions or libs such as parsec.
Note that this is NOT part of the assignment, I'm just interested in the way Haskell does things.