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 8935157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:56:05+00:00 2026-06-15T09:56:05+00:00

I am trying to write an interpreter for Brainf*ck in Haskell. But I am

  • 0

I am trying to write an interpreter for Brainf*ck in Haskell. But I am getting a type error.

Only the relevant parts of the code is shown below:

--my own defined data type
data BFState = BFState {
  program :: String,      -- program being interpreted
  input :: String,  -- input for the program
  memory :: [Word8],     -- memory is a list of 8bit representation of INTs since only least 8 bits are read
  prog_pointer :: Int,         -- current pointer in the program STRING (pc)
  mem_pointer :: Int         -- current pointer in the memory LIST (pos)
}

--Initialise the BFState before the Intepreter runs the inputted BF code.
initState :: String -> String -> Int -> BFState
initState program input memSize = BFState program input (take memSize (repeat 0)) 0 0

--Helper function for the main function (bf program input)
run state = if isEnd state
            then return () --when reached the end, just return
            else do newState <- (iterateBF state) --update to new state after iterating one BF comand
                    run newState --run with new state

--the main function
bf program input = run (initState program input 10000)

Now, I get a type error:

Couldn't match expected type `String'
            with actual type `BFState -> String'
In the first argument of `initState', namely `program'
In the first argument of `run', namely
  `(initState program input 10000)'
In the expression: run (initState program input 10000)

What is the compiler referring to when it says actual type 'BFState -> String' ?

—-For sepp2k

iterateBF :: BFState -> IO BFState
iterateBF state = case (program state !! prog_pointer state) of
    '+' -> return state {memory = setMem state ((getMem state) + 1), prog_pointer = nextPP state}
    '-' -> return state {memory = setMem state ((getMem state) - 1), prog_pointer = nextPP state}
    '>' -> return state {mem_pointer = (mem_pointer state) + 1, prog_pointer = nextPP state}
    '<' -> return state {mem_pointer = (mem_pointer state) - 1, prog_pointer = nextPP state}
    '[' -> return state {prog_pointer = prog_pointer'} where
            prog_pointer' = findClosingBrace (program state) (prog_pointer state)
    ']' -> return state {prog_pointer = prog_pointer'} where
            prog_pointer' = findOpeningBrace (program state) (prog_pointer state)
    ',' -> let inputVal = fromIntegral (fromEnum (head (input state))) in 
           return state {memory = setMem state inputVal, prog_pointer = nextPP state, input = drop 1 (input state)}
    '.' -> do hPutChar stdout (chr (fromEnum $ getMem state))
              hFlush stdout
              return state { prog_pointer = prog_pointer state}
    otherwise -> return (state {prog_pointer = nextPP state}) --ignore other characters

--check if we are at the end of the program
isEnd :: BFState -> Bool
isEnd state = (prog_pointer state) >= length (program state)
  • 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-15T09:56:07+00:00Added an answer on June 15, 2026 at 9:56 am

    That’s because the type of “program” (the first argument to your initState function) isn’t String. It’s actually BFState -> String, which you can verify by typing in “:t program” in the ghci prompt (without the quotes, of course).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a script in Go but I get this error:
I'm trying to write a brainfuck interpreter in Haskell as an exercise/fun project, and
While trying to write a tiny, obfuscated type checker, an unacceptable code pattern was
I m trying write code that after reset set up rrpmax as 3000. It
I am trying write a function that generates simulated data but if the simulated
Trying to write out syslog entries containing strings but they don't register. // person.name
Trying to write a code at the moment that basically tests to see if
I'm trying to write a toy python Scheme interpreter based on the meta-circular evaluator
I am trying to write a Python code analyzer, and I am trying to
I'm running Python 2.5.4 on Windows and I keep getting an error when trying

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.