为什么这个haskell代码不能被编译 [英] Why this haskell code cannot be compiled

查看:109
本文介绍了为什么这个haskell代码不能被编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编译一个haskell游戏代码,这段代码生成三个线程,一个用于无限循环,一个用于收集用户的交互,一个用于触发事件。然而,代码无法编译,我不知道为什么。



以下是代码:

<$ p $ 模块Main其中

导入Control.Concurrent
导入Control.Monad
导入System.IO
导入System.Random
import Text.Printf

data Msg = C Char |时间
data事件= C Char | Time Char

main :: IO()
main = do
hSetBuffering stdout NoBuffering
hSetBuffering stdin NoBuffering
hSetEcho stdin False

- 共享资源
chan< - newEmptyMVar
removedDigits< - newEmptyMVar
unmatchedDigits< - newEmptyMVar
numberOfGuesses< - newEmptyMVar

- 启动生成线程和用户线程
forkIO $ generateThread chan
forkIO $ userThread chan

- 主循环
if mainloop chan == True then 恭喜你,你赢了!否则下次更好运气!
return()

mainloop :: Chan c - > Bool
let mainloop = do
if length unmatchedDigits> = 10
then return False
Event< - readChan c
if Event ==timer
then unmatchedDigits ++ param
else if testGuessedNumber param unmatchedDigits == True
then
removeMatchedDigit
if length unmatchedDigits == 0
then return True
mainloop c

- 生成线程aka生成随机数的事件线程
generateThread :: Chan msgChan - > IO()
generateThread msgChan =永远$ do
publishTimerEvent msgChan 1000000

publishTimerEvent :: Chan msgChan - > Int delay()
publishTimerEvent msgChan = do
c< - getRandomChar
putMVar msgChan(timerc)
threadDelay newDelay
velocity < - 0.9
如果延迟*速度< 100
then newDelay< - 100
else newDelay< - delay * velocity
publishTimerEvent msgChan newDelay

getRandomChar :: Char c()
getRandomChar = do
i < - randomRIO(0,9)
让c =0123456789!! i
return c

- 用户线程
userThread :: MVar Msg - > IO()
userThread chan = forever $ do
c< - getChar
putMVar chan(C c)
showStr(show c)

testGuessedNumber: :Int - > Int - > Bool
testGuessedNumber a b
| a == b = True
|否则= False

- 在
后显示当前终端线左边缘的给定字符串 - 将该行的前20个字符消隐。
showStr :: String - > IO()
showStr s = putStr(\r++ replicate 20''++\r++ s)

错误是test.hs:36:3:error:parse error on input'Event'

解决方案

变量名称不能以大写字母开头,例如 Event 。尝试将该变量重命名为 event


I was trying to compile a haskell game code, this code generates three thread, one for infinite loop, one for collecting user's interaction, one for triggering the events. However, the code cannot be compiled and I don't know why.

Here's the code:

module Main where

import Control.Concurrent
import Control.Monad
import System.IO
import System.Random
import Text.Printf

data Msg = C Char | Time
data Event = C Char | Time Char

main :: IO ()
main = do
  hSetBuffering stdout NoBuffering
  hSetBuffering stdin NoBuffering
  hSetEcho stdin False

  -- shared resources
  chan <- newEmptyMVar
  removedDigits <- newEmptyMVar
  unmatchedDigits <- newEmptyMVar
  numberOfGuesses <- newEmptyMVar

  --starting the generating thread and the user thread
  forkIO $ generatingThread chan
  forkIO $ userThread chan

  --the main loop
  if mainloop chan == True then "Congratulations! You won!" else "Better luck next time!"
  return()

  mainloop :: Chan c -> Bool
  let mainloop = do
  if length unmatchedDigits >= 10 
    then return False
  Event <- readChan c
  if Event == "timer"
    then unmatchedDigits ++ param
    else if testGuessedNumber param unmatchedDigits == True 
      then 
        removeMatchedDigit 
        if length unmatchedDigits == 0
        then return True
  mainloop c

-- Generating Thread aka event thread generating the random numbers 
generatingThread :: Chan msgChan -> IO ()
generatingThread msgChan = forever $ do
  publishTimerEvent msgChan 1000000

publishTimerEvent :: Chan msgChan -> Int delay ()
publishTimerEvent msgChan = do
  c <- getRandomChar
  putMVar msgChan ("timer" c)
  threadDelay newDelay
  velocity <- 0.9
  if delay * velocity < 100
    then newDelay <- 100
    else newDelay <- delay * velocity
  publishTimerEvent msgChan newDelay

getRandomChar :: Char c ()
getRandomChar = do
  i <- randomRIO (0,9)
  let c = "0123456789" !! i
  return c

-- User Thread
userThread :: MVar Msg -> IO ()
userThread chan = forever $ do
  c <- getChar
  putMVar chan (C c)
  showStr(show c)

testGuessedNumber :: Int -> Int -> Bool
testGuessedNumber a b
    | a == b    = True
    | otherwise = False

-- Shows the given string at the left edge of the current terminal line after
-- having blanked out the first 20 characters on that line.
showStr :: String -> IO ()
showStr s = putStr ("\r" ++ replicate 20 ' ' ++ "\r" ++ s)

The error is "test.hs:36:3: error: parse error on input ‘Event’"

解决方案

Variable names can not begin with uppercase letters, such as Event. Try renaming the variable to something like event.

这篇关于为什么这个haskell代码不能被编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆