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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:49:25+00:00 2026-06-13T10:49:25+00:00

Here is a function that takes a pair of Integral values and divides them:

  • 0

Here is a function that takes a pair of Integral
values and divides them:

divide_v1 :: Integral a => (a, a) -> a
divide_v1 (m, n) = (m + n) `div` 2

I invoke the function with a pair of Integral
values and it works as expected:

divide_v1 (1, 3)

Great. That’s perfect if my numbers are always Integrals.

Here is a function that takes a pair of Fractional
values and divides them:

divide_v2 :: Fractional a => (a, a) -> a
divide_v2 (m, n) = (m + n) / 2

I invoke the function with a pair of Fractional
values and it works as expected:

divide_v2 (1.0, 3.0)

Great. That’s perfect if my numbers are always Fractionals.

I would like a function that works regardless of whether the
numbers are Integrals or Fractionals:

divide_v3 :: Num a => (a, a) -> a
divide_v3 (m, n) = (m + n) ___ 2

What operator do I use for _?

  • 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-13T10:49:26+00:00Added an answer on June 13, 2026 at 10:49 am

    To expand on what AndrewC said, div doesn’t have the same properties that / does. For example, in maths, if a divided by b = c, then c times b == a. When working with types like Double and Float, the operations / and * satisfy this property (to the extent that the accuracy of the type allows). But when using div with Ints, the property doesn’t hold true. 5 div 3 = 1, but 1*3 /= 5! So if you want to use the same “divide operation” for a variety of numeric types, you need to think about how you want it to behave. Also, you almost certainly wouldn’t want to use the same operator /, because that would be misleading.

    If you want your “divide operation” to return the same type as its operands, here’s one way to accomplish that:

    class Divideable a where
      mydiv :: a -> a -> a
    
    instance Divideable Int where
      mydiv = div
    
    instance Divideable Double where
      mydiv = (/)
    

    In GHCi, it looks like this:

    λ> 5 `mydiv` 3 :: Int
    1
    λ> 5 `mydiv` 3 :: Double
    1.6666666666666667
    λ> 5.0 `mydiv` 3.0 :: Double
    1.6666666666666667
    

    On the other hand, if you want to do “true” division, you would need to convert the integral types like this:

    class Divideable2 a where
      mydiv2 :: a -> a -> Double
    
    instance Divideable2 Int where
      mydiv2 a b = fromIntegral a / fromIntegral b
    
    instance Divideable2 Double where
      mydiv2 = (/)
    

    In GHCi, this gives:

    λ> 5 `mydiv2` 3
    1.6666666666666667
    λ> 5.0 `mydiv2` 3.0
    1.6666666666666667
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Is there a Max function in SQL Server that takes two values
Here is a Haskell function that takes a number n and returns the nth
Here is my problem, I need to create a javascript function that takes 2
I am attempting to create a generic mapping function that takes the values of
I'm trying to write a PS function that takes a -Computername parameter. Here is
Here is the function that i am using: $('.urlz').click(function (event) { event.preventDefault(); var url
I've got a function here that is meant to look through a list of
Here is a simple function that converts a string to an integer. int str2int(char
Here is an (artificial) example of using a function that returns an anonymous struct
Here's a brief rundown of my issue: I have a JavaScript function that gets

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.