Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8241969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:00:53+00:00 2026-06-07T21:00:53+00:00

I’m trying to write a brainfuck interpreter in Haskell as an exercise/fun project, and

  • 0

I’m trying to write a brainfuck interpreter in Haskell as an exercise/fun project, and I’ve run into a little problem.

Brainfuck’s “while loop” structure is just a series of commands stuck inside of brackets. I’m trying to build up the syntax tree in a way that I store operators inside loops inside of the [ data constructor.

This is how the data declaration for the commands and syntax “tree” look at the moment:

data Operator = Plus
                | Minus
                | RShift 
                | LShift
                | Dot
                | Comma
                | SBracket [Operator]
                | EBracket
                deriving (Show, Eq)


type STree = [Operator] 

What I’m trying to do is take a String of commands like "+><[.>]" and parse it into an STree that looks like this:

[Plus, RShift, LShift, SBracket [Dot, RShift], EBracket]

So far, I’m only able to get a one-dimensional list out of the String, because I’m not sure how to check if the head of the list is a SBracket in order to put new operators in it’s operator list instead of at the head of the main list.

Here’s the function I’m using to do the parsing:

matchChar :: Char -> Maybe Operator
matchChar c = case c of 
                '+' -> Just Plus
                '-' -> Just Minus
                '>' -> Just RShift  
                '<' -> Just LShift
                '.' -> Just Dot
                ',' -> Just Comma
                '[' -> Just (SBracket [])
                ']' -> Just EBracket
                _   -> Nothing

getChars :: [Char] -> STree
getChars str = foldr toOp [] str
    where 
            toOp x acc = case matchChar x of
                            Just a -> a:acc
                            Nothing -> acc

What I’d like to be able to do is check if head acc is an SBracket instance, and if so, instead of prepending the new Operator to the list, prepend it to SBracket‘s Operator list.

I’ve tried pattern matching (toOp x ((SBracket list):xs) = ...) as well as trying to explicitly check the head of the list (if head acc == SBracket ...), but neither of these things work properly.

Any help would be great!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:00:54+00:00Added an answer on June 7, 2026 at 9:00 pm

    First off, I’d redefine SBracket [Operator] as just Bracket STree and get rid of EBracket. Then I’d change your parser to keep track of both the “current STree” as well as a list of parents. Every time you encounter a bracket, you push the current tree onto your parent list and create a new tree. And when you encounter an end bracket, you take your current tree, wrap it with the Bracket constructor, pop off your first parent, add the bracket to the end of that and make that your current tree.


    Here’s a wholly untested (don’t have ghc on this comp) version that may or may not work:

    data Operator = Plus
                    | Minus
                    | RShift
                    | LShift
                    | Dot
                    | Comma
                    | Bracket [Operator]
                    deriving (Show, Eq)
    
    parse :: [Char] -> Either String [Operator]
    parse str = parse' str [] []
      where parse' :: [Char] -> [Operator] -> [[Operator]] -> Either String [Operator]
            parse' [] context [] = Right (reverse context)
            parse' [] context _ = Left "unclosed []"
            parse' (']':cs) _ [] = Left "unexpected ]"
            parse' (c:cs) ctx stack
                | c == '+' = parse' cs (Plus:ctx) stack
                | c == '-' = parse' cs (Minus:ctx) stack
                | c == '>' = parse' cs (RShift:ctx) stack
                | c == '<' = parse' cs (LShift:ctx) stack
                | c == '.' = parse' cs (Dot:ctx) stack
                | c == ',' = parse' cs (Comma:ctx) stack
                | c == '[' = parse' cs [] (ctx:stack)
                | c == ']' = parse' cs (Bracket (reverse ctx):s) tack
                | otherwise = parse' cs ctx stack
              where (s:tack) = stack
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I am trying to loop through a bunch of documents I have to put
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.