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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:26:55+00:00 2026-06-17T05:26:55+00:00

The code bellow works pretty well for input like this: *Main> eval ‘y’ (21

  • 0

The code bellow works pretty well for input like this:

*Main> eval 'y' (21 :: Int)
42
*Main> eval 'x' 'a'
42
*Main> eval (21 :: Int) (21 :: Int)
42
*Main> eval (42 :: Int) 'a'
42

The general problem behind this is that I want to add two things. Adding to Ints is not hard to realize (it’s already built in). But I’ve a given function (here geti) which resolves Chars to Ints and my add-function now should add two Ints as well as Int combined with Char (in every permutation). The Chars are converted by the geti function to Ints so that they can be added. I’ve thought about an other solution which might be:

eval (geti 'a') (42 :: Int)

but that is not possible for me.

So in general my question is: is there any way to make this simpler or implement it more elegant?

This is the code:

-- A static function only used to resolve some demo data
geti :: Char -> Int
geti c | c == 'x' = 42
       | c == 'y' = 21
       | c == 'z' = 10
       | otherwise = 0


-- Here comes the problem:
class Eval t1 t2 where 
    eval :: t1 -> t2 -> Int

instance Eval Int Int where
    eval a b = a + b

instance Eval Int Char where
    eval a c = a + (geti c)

instance Eval Char Int where
    eval c b = (geti c) + b

instance Eval Char Char where
    eval c1 c2 = (geti c1) + (geti c2)

P.S.: I’ve also tried to combine the solution from here with an “generic” (yes, it’s Java language, but I’m new to Haskell … sry) function but that didn’t work …

  • 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-17T05:26:56+00:00Added an answer on June 17, 2026 at 5:26 am

    I think it would be more straight forward to build a type class for “can be converted to Int” and use that to implement eval:

    class Intable a where
       intify :: a -> Int
    
    instance Intable Int where
       intify = id
    
    instance Intable Char where
       intify = geti
    
    
    eval :: (Intable a, Intable b) => a -> b -> Int
    eval a b = intify a + intify b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below works well right now; however, it's serially executed. I'd like to
This code below works in all web browsers except IE: <input type="text" name="passwordLogin" value="Password"
The code below works well but grabs only the UIView which is VISIBLE on
I have the code below and it works pretty good except if you enter
I'm performing a rotation of a 2d canvas which works pretty well on the
The code below works for Delphi XE, but the 2400 buffersize is pretty ugly.
I have a small little tip calculator app that works pretty well, i am
Initially, this code was written to create pretty urls. When using the code below,
the code below works in Windows Phone 7 private void ShowTime() { txtTime.Text =
The code below works fine. But it seem clunky. How would you combine the

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.