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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:30:42+00:00 2026-05-26T16:30:42+00:00

(Please forgive me in advance for any formatting errors, as this is the first

  • 0

(Please forgive me in advance for any formatting errors, as this is the first time I’ve asked a question here.)

I’m working on an interpreter for the While language for class, and I think I have a pretty good handle on the ideas behind it and what I need to do. Some code was provided for us (the eval functions for the Expr type) and we have to implement the eval for the Statement type and the symbol lookup functionality for assignments.

My current problem is that I’m getting type errors while trying to do the above and I’m not sure why.

Here’s the relevant code (note that the While eval is set to = 0 intentionally at the moment):

data Expr = Num Integer
      | Var String
      | Bin Op Expr Expr deriving Show

data Statement = Assign String Expr
                | If Expr Statement Statement
                | While Expr Statement
                | Compound [Statement] deriving Show

env = [("n",1), ("fact", 1)]

eval (Num x) _ = x
eval (Var v) e = xlookup v e
   where xlookup v ((w, x):r) | v==w = x
                              | otherwise = xlookup v r
eval (Bin op l r) e = kop op (eval l e) (eval r e)
  where kop Mul x y = x * y
        kop Sub x y = x - y
        kop Add x y = x + y
        kop Gt  x y | x > y = 1
                    | otherwise = 0

eval (While exp s) e = 0

factorial = (Compound [
               (Assign "n" (Num 7)),
               (Assign "fact" (Num 1)),
               (While (Bin Gt (Var "n") (Num 1))
                  (Compound [
                      (Assign "fact" (Bin Mul (Var "fact") (Var "n"))),
                      (Assign "n" (Bin Sub (Var "n") (Num 1)))
                  ])
               )
            ])

Here’s what I get:

Hugs> :l while.hs
ERROR "while.hs":37 - Type error in function binding
*** Term           : eval
*** Type           : Statement -> [([Char],Integer)] -> Integer
*** Does not match : Expr -> [([Char],Integer)] -> Integer

Basically, every attempt I make at an eval function for a Statement, Haskell seems to be expecting an Expr instead and I’m not sure why. I’m sure this is a very basic question but unfortunately I’m very new to Haskell and apparently still pretty terrible at it. Thanks for any help.

  • 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-05-26T16:30:42+00:00Added an answer on May 26, 2026 at 4:30 pm

    You either need to make eval a method of a typeclass, which I think is overkill at this point, or make separate eval functions for your Expr and Statement types:

    eval_expr (Num x) = ...
    eval_expr (Var x) = ...
    eval_expr (Bin op l r) = ...
    
    eval_stmt (While expr x) = ...
    

    The typeclass method would look like:

    class Eval a where
        eval :: a -> [([Char], Integer)] -> Integer
    
    instance Eval Expr where
        eval (Num x) = ...
        ...
    
    instance Eval Statement where
        eval (While expr x) = ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[please forgive formatting errors-- first post here and I tried earnestly to do it
Please forgive if this question has been asked numerous times. I recently installed Eclipse
Please forgive any short-comings in this question; flash is not my area of expertise,
Please forgive me up front. When I've tried to research this question I end
Please forgive me if any of this is wrong -I'm completely new to Java.
Please forgive the awkwardness of this question, I honestly don't know how to phrase
XML/XSL newbie here, so please forgive my ignorance. This is the xml input given
Please forgive me if this is a dumb question. I'm only a week into
Please forgive me if this question is ridiculous. I'm relatively new to programming and
Please forgive my long question. I have an idea for a design that I

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.