Closed IanMarsh1 closed 1 year ago
Good question, let's break it down a little at a time:
suite
function needs to be a parser for a list of Statement
s (i.e., returns a [Statement]
wrapped in an IParser
).block statement
, you're building a parser for a list of Statement
s (because statement
is a parser for a single Statement
and block
converts that into a parser for [Statement]
).suite
can also be a StmtList
, but the stmtList
function gives you a parser for a Statement
(i.e., returns a Statement
wrapped in an IParser
), which is the wrong type since you need [Statement]
.StmtList
kind of statement does contain a [Statement]
, so what you need to do is get the [Statement]
from inside the StmtList
and return it.Here's a simple, but kind of analogous, Haskell example showing how to get the data encapsulated in one type so that we can pass it along. In this example, the IO
type is taking the place of IParser
.
-- Imagine a data type that can encapsulate different kinds of values, includes itself
data Result = AResult Int | ManyResults [Result]
-- It can sometimes be helpful to have a function that can extract an encapsulated value
getResults :: Result -> [Result]; getResults r = case r of (ManyResults rs) -> rs; _ -> [r]
-- Now suppose we have a way of generating an instance of our data type
readResults :: IO Result
readResults = (ManyResults . map (AResult . read) . words) <$> getLine
-- But perhaps we also need a way to generate something like what's encapsulated in our type
-- One way we can do so is to extract the encapsulated data using our helper function
readResultList :: IO [Result]
readResultList = getResults <$> readResults
In the example above, the Result
type is kind of like our Statement
type, the readResults is a bit like
stmtListand
readResultListplays the role of
suite` (or at least the part of that function where you are trying to parse a statement list).
A bit lost in the sauce. Can you tell me if I am on the right path or not?
Sorry about that last push did it by accident. Also kind of shooting from the hip with this. I understand that I am trying to return [Statement], and stmtList is just a Statement. I think that adding block before it makes stmtList return a list, but I am still a little confused.