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

  • Home
  • SEARCH
  • 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 713617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:58:53+00:00 2026-05-14T04:58:53+00:00

I wrote a little Haskell program to find the area of a triangle, primarily

  • 0

I wrote a little Haskell program to find the area of a triangle, primarily to practice custom types, but it keeps throwing the following error on compile:

areafinder.hs:7:4:
    Couldn't match expected type `Triangle' against inferred type `m b'
    In a stmt of a 'do' expression: putStr "Base: "
    In the expression:
        do { putStr "Base: ";
             baseStr <- getLine;
             putStr "Height: ";
             heightStr <- getLine;
             .... }
    In the definition of `getTriangle':
        getTriangle = do { putStr "Base: ";
                           baseStr <- getLine;
                           putStr "Height: ";
                           .... }

I’m not sure where ‘m b’ comes from, so I’m at a loss here. Why is it throwing this error, and what can I do to fix it? Here is my code:

module Main where

data Triangle = Triangle Double Double -- base, height
getTriangle :: Triangle
getTriangle = do
    putStr "Base: "
    baseStr <- getLine
    putStr "Height: "
    heightStr <- getLine
    let base = read baseStr :: Double
    let height = read heightStr :: Double
    Triangle base height

calcTriangle :: Triangle -> Double
calcTriangle (Triangle base height) = base * height

main = putStrLn ("Area = " ++ show (calcTriangle getTriangle))

Thanks. 🙂

  • 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-14T04:58:53+00:00Added an answer on May 14, 2026 at 4:58 am

    The getTriangle function uses IO, so you have to put that in the function signature.

    getTriangle :: IO Triangle
    

    Also, the last line should have return, since it’s returning a pure value inside an IO function.

    return (Triangle base height)
    

    Here are a couple extra tips: Haskell can figure out that base and height are Double, because you pass them to Triangle, so you don’t need to explicitly declare them that way. You can use liftM from the Control.Monad module to read the input and convert to Double in one step.

    import Control.Monad
    getTriangle :: IO Triangle
    getTriangle = do
        putStr "Base: "
        base <- liftM read getLine
        putStr "Height: "
        height <- liftM read getLine
        return (Triangle base height)
    

    The main function also appears to mix pure values with IO. Since getTriangle is IO, you can’t pass it directly to calcTriangle. Here is a modified main:

    main = do tri <- getTriangle
              putStrLn ("Area = " ++ show (calcTriangle tri))
    

    As a footnote, the area of a triangle is base * height / 2, not base * height.

    Finally, a more advanced Haskell programmer would probably write getTriangle in terms of liftM2, but this is just a matter of style. Here is how I would write it:

    prompt str = putStr (str ++ ": ") >> liftM read getLine
    getTriangle = liftM2 Triangle (prompt "Base") (prompt "Height")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ported a little Haskell program I wrote from Mac to Windows. It's a
I wrote a little test program but I'm experiencing a syntax error in my
I wrote a little program that solves 49151 sudoku's within an hour for an
I wrote a little program to do generalized caesar ciphers. I can't figure out
I'm trying to make a little program in Haskell. What I need to do
I wrote a little program to draw a pattern a cycle through different color
I wrote this little program in c++ to in order check CPU load scenarios.
I wrote a little program to calculate the first 18 triples (x,y,z) with x<y<z
I wrote this little snippet that should format money but its failing on the
So I wrote a little multithreaded SMTP program. The problem is every time 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.