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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:17:49+00:00 2026-06-04T12:17:49+00:00

The following is my dining philosophers code and yields a compilation error saying The

  • 0

The following is my dining philosophers code and yields a compilation error saying “The last statement in a ‘do’ construct must be an expression: mVar2 <- newEmptyMVar mVar3”
Can Somebody help me fix this error and get this program working? thank you

import Control.Concurrent
import Control.Concurrent.MVar
import System.Random

takefork :: Int -> forks -> IO ()
takefork n forks = takeMVar (forks!!n)

releasefork :: Int -> forks -> IO ()
releasefork n forks = putMVar (forks!!n)

philosopher :: [Int]
philosopher = [1,2,3,4,5]

forks :: [MVar] -> [Int]
forks = do
    takefork n ( philosopher - 1)
    threadDelay delay
    let delay = 100000
    takefork n philosopher
    putStrLn("Philosopher" ++ philosopher ++ "has started eating")
    releasefork n philosopher
    releasefork n ( philosopher - 1)
    ptStrLn ("Philosopher" ++ philosopher ++ "has stopped eating")
    forks

main :: IO ()
main = do
    mVar1 <- newEmptyMVar
    mVar2 <- newEmptyMVar
    mVar3 <- newEmptyMVar
    mVar4 <- newEmptyMVar
    mVar5 <- newEmptyMVar
    let mVar = [mVar1, mVar2, mVar3, mVar4, mVar5]
    sequence_ [ forkIO forks (mVar philosopher) ]
  • 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-04T12:17:51+00:00Added an answer on June 4, 2026 at 12:17 pm

    There are many problems with your code.

    The error message you report indicates you are probably mixing spaces and tabs. Get rid of the tabs and use only spaces.


    You are presumably writing this program in order to practice writing Haskell programs, not in order to run the program for fun and profit. So we don’t want to simply give you a working Dining Philosophers implementation, we want to help you write your implementation.

    I cannot tell from your code how you expect it to work.

    I’m going to focus on the last line:

    sequence_ [ forkIO forks (mVar philosopher) ]
    

    sequence_ :: [IO a] -> IO () — give sequence_ a list of i/o actions, and it (returns an i/o action that) performs each action in order. From the [...], it looks like you are trying to give it a list, but with only one element. This is probably not what you mean.

    forkIO :: IO () -> IO ThreadID — give forkIO an i/o action, and it (returns an i/o action that) starts that i/o action running in a new thread, giving you the id of that thread.

    There are two problems here:

    • forks is a function, not an i/o action (it’s not even a function that returns an i/o action, though you probably mean it to be)
    • you give forkIO a second argunment ((mVar philosopher)), but it only takes one argument

    mVar philosopher itself doesn’t make any sense: mVar :: [MVar a] (it’s a list of MVars, and I haven’t worked out what type the MVars are supposed to contain) but you treat it like a function, passing it philosopher as an argument.

    At this point a lightbulb blinks on above my head. You wish to call forks with parameters mVar and philosopher?

    sequence_ [ forkIO (forks mVar philosopher) ]
    

    We’re still sequencing a single action though. Perhaps you wish to call forks with each element of philosopher in turn?

    sequence_ $ map (\n -> forkIO (forks mVar n)) philosopher
    

    We can simplify this to

    mapM_ (\n -> forkIO (forks mVar n)) philosopher
    

    This doesn’t match up with the type you given forks :: [MVar] -> [Int]. But that’s probably wrong, so you’ll want to fix that function next.

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

Sidebar

Related Questions

Following example code from the libpcap documentation yields the following code which should report
Following on from my last question : I've written some code to upgrade a
Following Uncle Bob's avice in Clean Code , I'd like to have no SQL
I'm currently diving into node.js stuff starting with very simple examples. The following code
Following is my code: <table border=0 align=center cellpadding=0 cellspacing=0 class=grid_table> <tr class=grid_caption><td class=grid_caption>Customer Name</td></tr>
The following code doesn't appear to work properly for me. It requires starting a
my application has the following code: public interface IConfigurationManager { CustomSection Settings { get;
following piece of code: class a{ public function __get($key) { if($key == 'obj') {
At the moment I am using the following bit of code to only get
Following snippet wouldn't compile. With following error: Cannot implicitly convert type 'Container<ChildClass>' to 'Container<BaseClass>'

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.