2016-07-10 17 views
2

我想用haskell製作一個非常簡單的twitter客戶端,並且使事情變得簡單我正在嘗試做一個簡單的putStrLn和getLine(我不知道這是否是最好的解決方案對於這個問題或不是,我對haskell相當陌生)。小嘰嘰喳喳客戶端的Haskell IO cli菜單

我想要做這樣的事情,但輸出類型是不同的,所以它要給一個巨大的錯誤:

main:: IO() 
main = do 
    putStrLn "1)Tweet\n 2)Timeline\n 3)DM\n 4)Inbox\n" 
    numero <- getLine 
    if(numero == 1) 
     then do 
      frase <- getLine 
      tweet frase 
     else 
      if(numero == 2) 
       then do 
        frase <- getLine 
        timeline frase 
      else 
       if(numero == 3) 
        then do 
         frase <- getLine 
         nome <- getLine 
         dm frase nome 
        else 
        if(numero == 4) 
          then 
           inbox 
          else do 
            PutstrLn "Invalido" 


tweet :: String -> IO (Either String Tweet) 

timeline :: String -> IO (Either String [Tweet]) 

dm :: String -> String -> IO(Either String DM) 

inbox :: IO(Either String [DM]) 

就像我它去上面解釋給你喜歡的錯誤:

Main.hs:86:25: error: 
    Couldn't match type ‘Either String DM’ with ‘()’ 
    Expected type: IO() 
     Actual type: IO (Either String DM) 

和:

Main.hs:75:11: error: 
• Couldn't match type ‘Either String Tweet’ with ‘()’ 
    Expected type: IO() 
    Actual type: IO (Either String Tweet) 

如果有人有意識解決這個特定的公關它會非常感激。

+0

請發佈所有相關的代碼和錯誤消息。如果您的文章沒有提供[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve),那麼很難爲您提供幫助。 – Mephy

+0

@Mephy添加了一些示例 –

+0

問題中存在拼寫錯誤。 'PutstLn'應該是'putStrLn'。 – Cirdec

回答

2

如果你的IO操作返回一個值,如inbox :: IO(Either String [DM]),但你想它需要有一個IO()可以忽略返回值,並用return()具有類型IO()跟着它使用它。

if(numero == 1) 
     then do 
      frase <- getLine 
      tweet frase 
      return() 
     else 
      ... 

旁白:您可以通過使用一個case expression代替嵌套if ... then ... else小號簡化這個縮進。

main:: IO() 
main = do 
    putStrLn "1)Tweet\n 2)Timeline\n 3)DM\n 4)Inbox\n" 
    numero <- readLn 
    case numero of 
     1 -> do 
      frase <- getLine 
      tweet frase 
      return() 
     2 -> do 
      frase <- getLine 
      timeline frase 
      return() 
     3 -> do 
      frase <- getLine 
      nome <- getLine 
      dm frase nome 
      return() 
     4 -> do 
      inbox 
      return() 
     otherwise -> do 
      putStrLn "Invalido" 

我也換成getLine :: IO StringreadLn :: Read a => IO a

+0

我不能很感謝你,很好的解釋,10出10 –