I / O我怎么能在屏幕上放一些东西而不是字符串?

时间:2018-01-20 10:00:02

标签: haskell

所以我正在做这个功能,我需要她在屏幕上显示(premio ap x)的结果,问题是(premio ap x):: Maybe Int,所以它不是一个字符串。

   joga :: Aposta -> IO ()

    joga x= do
            ap <- leAposta;
            let arroz = (premio ap x)
            putStr  ^^^^^^^^^^
            return ()

如何将此转换为字符串?或者还有另一种方法可以在屏幕上显示非字符串的内容。

更新:完整代码

  comuns :: Aposta -> Aposta -> (Int,Int)
  comuns (Ap a (b,c)) (Ap k (l,ç)) = (cnum a k, cnum [b,c] [l,ç])


  cnum::[Int]->[Int]->Int
  cnum [] l2 = 0
  cnum (x:xs) l2 | elem x l2 = 1 + cnum xs l2
           |otherwise = cnum xs l2


  premio :: Aposta -> Aposta -> Maybe Int
  premio l1 l2 | x == (5,2)= Just 1
         | x == (5,1)= Just 2
         | x == (5,0)= Just 3
         | x == (4,2)= Just 4
         | x == (4,1)= Just 5
         | x == (4,0)= Just 6
         | x == (3,2)= Just 7
         | x == (2,2)= Just 8
         | x == (3,1)= Just 9
         | x == (3,0)= Just 10
         | x == (1,2)= Just 11
         | x == (2,1)= Just 12
         | x == (2,0)= Just 13
         |otherwise = Nothing
where 
    x = comuns l1 l2



   leAposta :: IO Aposta
   leAposta = do 
        putStrLn "Insira como lista as 5 estrelas"
        num <-getLine
        putStrLn "Insira em par as 2 estrelas"
        es<-getLine
        let ap = (Ap (read num) (read es))
        if (valida ap) 
            then return ap
        else do 
            putStrLn "Aposta invalida"
            leAposta

1 个答案:

答案 0 :(得分:0)

由于arrozpremio ap x且类型为Maybe Int,因此您只需print arroz

print适用于任何可以打印的类型,即Show类中的那些类型。

(您可能不希望对已经是字符串的值使用print,因为这将打印带有引号的转义字符串。使用{{1} }和putStr表示字符串。)