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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:52:39+00:00 2026-05-20T23:52:39+00:00

I thought I was beginning to understand IO in Haskell until I ran into

  • 0

I thought I was beginning to understand IO in Haskell until I ran into the following problem.

I have the following function, which returns type IO Float:

getFundPrice :: Int ->  Int -> IO Float
getFundPrice fund date = do 
                       priceList <- getFundPrice' fund date
                       let h = head priceList
                       return  h

The function getFundPrice’ uses the takusen database library and returns a list of type IO [Float].

I’m able to successfully test the getFundPrice function with Hunit using the following:

  p <- getFundPrice 120 20100303
  assertEqual
    "get fund price"
    10.286 
    (p)

The problem that is stumping me is the following function definition:

lastPos :: Int -> (Shares,Float) -> Period -> Fund -> (Shares,Float)
lastPos endDate begPos [] fund  = (fst begPos, trnPrice) 
                               where trnPrice = do
                                             price <- getFundPrice fund endDate
                                             return price                                                                        

The error I’m getting when attempting to compile is ” Couldn’t match expected type Float' against inferred typeIO Float'”

I thought the *price <- getFundPrice * action would retrieve the price for me as it does with my HUnit code.

What’s different about using this in the where clause?

  • 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-20T23:52:40+00:00Added an answer on May 20, 2026 at 11:52 pm

    If you remove the explicit type signature you will see the correct type of:

    lastPos :: Int -> (Shares,Float) -> Period -> Fund -> (Shares,IO Float)
    

    Notice the IO Float at the end. You have defined trnPrice to be an IO action that retrieves a floating point value. You have not executed that action to retrieve the value! You can not execute that action from anywhere except for the IO monad, which lastPos is not in. What you can do is:

    lastPos :: Int -> (Shares,Float) -> Period -> Fund -> IO (Shares, Float)
    lastPos endDate begPos [] fund  = do
        price <- getFundPrice fund endDate
        return (fst begPos, price)
    

    Which lifts all of lastPos into IO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought .Net code gets compiled into MSIL, so I always wondered how do
I don't understand, why does the following regular expression: ^*$ Match the string 127.0.0.1?
As far as I understand it, there is no way in Scala to have
So I understand that a line such as: find /var/log/ -mtime +60 -type f
NOTE: I thought I was using bash, but /bin/sh is linked to /bin/dash, which
I am in the process of designing a web application which will have multiple
I thought I knew how to use arrays until I started storing form filling
I have scoured SE and google and found what I thought were decent examples
Problem: Current regex pattern does not filter all lines. Adding ^ to the beginning
I thought jQuery Intellisense was supposed to be improved with SP1. I even downloaded

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.