简体   繁体   English

Haskell无限循环

[英]Haskell infinite loop

I am implementing a calculator in Haskell to brush up on the language but I am hitting a snag in my main when I want it to enter an infinite loop until the user inputs q . 我正在Haskell中实现一个计算器以重读该语言,但是当我希望它进入无限循环直到用户输入q时,我遇到了一个障碍。 Heres my main let me know if you see what I'm doing wrong and ill also post my error 这是我的主要负责人,如果您看到我在做错事并且生病了,也请发给我错误

error: No instances for (Floating (IO a0), Read (IO a0))
      arising from a use of `compute'
    Possible fix:
      add instance declarations for (Floating (IO a0), Read (IO a0))
    In a stmt of a 'do' block: compute e
    In the expression:
      do { compute e;
           evaluate_input }
    In an equation for `evaluate_expression':
        evaluate_expression e
          = do { compute e;
                 evaluate_input }

In your do-statement 在你的陈述中

 compute e 
 evaluate_input

both function need to be of the same monadic type, in here IO (declared by evaluate_input :: IO () ). 这两个函数必须具有相同的单子类型,在此处为IO (由evaluate_input :: IO () )。 So GHC now can expect that compute is a function that takes the String e and returns an IO a0 ( == a ). 因此,GHC现在可以期望compute是一个接受String e并返回IO a0== a )的函数。 Yet, it could not find any a0 so that IO a0 is an instance of Floating or Read , which a must be. 然而,它找不到任何a0使IO a0就是一个实例FloatingRead ,其中a必须是。

I'd assume that you want to output the result of the computation (and a is an instance of Show rather than Read ), so use 我假设您要输出计算结果(并且aShow的实例,而不是Read ),因此请使用

do
 putStrLn . show $ compute e
 evaluate_input

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM