For an assignment, we had to implement something like a very basic sexp parser, such that for input like:
"((a b) ((c d) e) f)"
It would return:
[["a", "b"], [["c", "d"], "e"], "f"]
Since this was part of a larger assignment, the parser is only given valid input (matching parens &c). 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
Which may not be the best solution, but it does the job.
Now, I’m interested in an idiomatic Haskell solution for the core functionality (i.e. I don’t care about the lexing or choice of delimiters, taking already lexed input would be fine), if possible using only “core” haskell, without extensions or libs like parsec.
Note that this is NOT part of the assignment, I’m just interested in the Haskell way of doing things.
Does not have a valid type in haskell (because all elements of a list need to be of the same type in haskell), so you’ll need to define your own datastructure for nested lists like this:
Now if you have a list of Tokens where Token is defined as
data Token = LPar | RPar | Symbol String, you can parse that into a NestedList like this: