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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:42:33+00:00 2026-06-03T14:42:33+00:00

I am working through the amazing Write Yourself a Scheme in 48 Hours and

  • 0

I am working through the amazing Write Yourself a Scheme in 48 Hours and have completed the core tasks and wanted to extend it but ran into problem. What I wanted to do is make eval function available to runtime, but have issue storing it into the global environment.

The runtime environment is of type:

type Env = IORef [(String, IORef LispVal)]

The Haskell eval implementation is of type:

eval :: Env -> LispVal -> IOThrowsError LispVal

The global environment is a mapping of type:

primitiveBindings :: IO Env

as it contain functions performing IO mixed with pure functions. My attempt was to set the runtime eval to the host eval partially applied with global environment like this:

baseFun :: [(String, [LispVal] -> IOThrowsError LispVal)]
baseFun = [("eval", unaryOp (eval (liftIO $ readIORef primitiveBindings)))]

Where unaryOp is:

unaryOp :: (LispVal -> ThrowsError LispVal) -> [LispVal] -> ThrowsError LispVal
unaryOp f [v] = f v

I wanted to then add the elements into the global environment but I get a compile error of:

Couldn't match expected type `IORef a'
       against inferred type `IO Env'
In the first argument of `readIORef', namely `primitiveBindings'
In the second argument of `($)', namely `readIORef primitiveBindings'
In the first argument of `eval', namely
    `(liftIO $ readIORef primitiveBindings)'

It appear that this pattern of readIORef env happen often in the code, so it unclear why it’s not working here. I would greatly appreciate enlightenment in my errors. For reference my code is almost exactly like the final code for the original tutorial as a reference.

Thank you

  • 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-03T14:42:35+00:00Added an answer on June 3, 2026 at 2:42 pm

    As far as I can tell, primitiveBindings is not a global environment, but rather an action that, when performed, generates a fresh mapping with the primitive bindings already set up. Perhaps a better name for it would have been createPrimitiveBindings. There is no way to have proper globals in Haskell without unsafePerformIO hacks.

    Therefore, the way you’re trying to add eval would make it evaluate everything in a new environment, since you’re re-running the primitiveBindings action. We can easily take care of the type errors, if that is indeed what you intended:

    baseFun :: [(String, [LispVal] -> IOThrowsError LispVal)]
    baseFun = [("eval", evalInNewEnv)]
        where evalInNewEnv args = do
                  env <- liftIO primitiveBindings
                  unaryOp (eval env) args
    

    As you see, there is no need for any readIORef here, since eval already expects Env which just a type synonym for an IORef.

    However, it sounds to me like you’d like eval to work in a “global” environment, in which case, you would need to have this environment passed to you somehow, since as I mentioned, there are no globals. For example, we can define something like this which would create a new environment with eval in it.

    primitiveBindingsWithEval :: IO Env
    primitiveBindingsWithEval = do
        env <- primitiveBindings    
        bindVars env [("eval", unaryOp (eval env))]
    

    You can then replace existing uses of primitiveBindings with primitiveBindingsWithEval when you want a fresh environment with eval in it.

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

Sidebar

Related Questions

I am working through the Agile Web Development with Rails book but I have
Currently working through a Teach Yourself WPF tutorial. Usually I can mentally convert from
I am working through a REST services book dealing with WCF. I have been
Working through the summer of nHibernate tutorials have gotten to the section on queries.
I'm working through K & R to learn programming. Going well so far, but
I am working through an MVVM tutorial, and I have the following code, written
When working through MathLink with slave kernel I have a problem with correct parsing
I've been working through Practical Common Lisp and as an exercise decided to write
Still working through Programming Collective Intelligence and using Clojure to write the code. I've
Currently working through a tutorial on JOX included within Sams Teach Yourself JavaServer Pages

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.