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

The Archive Base Latest Questions

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

I am trying to implement the law of cosines function, and here is my

  • 0

I am trying to implement the law of cosines function, and here is my code:

cosC :: [a] -> a
cosC sides
   | length sides < 3          = 0
   | otherwise                 = (x ^ 2 + y ^ 2 - z ^ 2) / (2 * x * y)
   where x = head(tail(tail(sides)))
         y = head(tail(sides))
         z = head(sides)

But I get two errors:

No instance for (Fractional a)
arising from a use of `/'
In the expression: (x ^ 2 + y ^ 2 - z ^ 2) / (2 * x * y)
In an equation for `cosC':
    cosC sides
      | length sides < 3 = 0
      | otherwise = (x ^ 2 + y ^ 2 - z ^ 2) / (2 * x * y)
      where
          x = head (tail (tail (sides)))
          y = head (tail (sides))
          z = head (sides)

and

No instance for (Num a)
arising from the literal `2'
In the first argument of `(*)', namely `2'
In the first argument of `(*)', namely `2 * x'
In the second argument of `(/)', namely `(2 * x * y)'

Edit: I have fixed the sign typo in the law of cosines above. Thanks to Daniel Fischer for pointing that out.

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

    You’re trying to calculate numerical results out of general types a, that can’t possibly work. (It’s like trying to build a bridge not just for general road-vehicles but for general things, e.g. spaceships, skyscrapers, paper clips and neutron stars). Just add the Floating constraint to a:

    cosC :: Floating a => [a] -> a
    

    and you can perform any of the arithmetic operations you need for such a calculation. (Fractional is actually enough for this function, but you won’t be able to calculate the arccos of the result then).


    Unrelated to your problem, note that there’s a much better way to decompose lists in Haskell:

    cosC (x:y:z:_) = (x^2 + y^2 - z^2) / (2*x*y)
    cosC _ = 0
    

    is equivalent to your definition. Why are you taking the arguments as a list anyway? That’s quite a Lisp-ish thing to do, in Haskell I’d prefer

    cosC :: Floating a => a -> a -> a -> a
    cosC x y z = (x^2 + y^2 - z^2) / (2*x*y)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to implement AVAudioplayer and get some metering data of the played music, but
Trying to implement the decorator pattern in C# from the code in the Head
trying to implement the following but its not working <script> function loadmap(lat,lng) { //alert(lat,lang);
I am trying implement rsync with python. Here's my code. #!/usr/bin/python import os url
Trying to implement a search similar to here .This searches properties based on city,locality,property
I'm trying implement server code of Server-Sent Events in a generic way that any
Ok well I'm trying implement something similar to the 'undo' function in many image
Trying to implement a function that will return a list of ints the represent
SetFocus I'm trying implement the above Se Focus code in a Class Library that
Trying to implement this gallery on my website. http://coffeescripter.com/code/ad-gallery/ It is noted in 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.