2016-08-20 6 views
0

作品: は、実際の型と予想される型 'IOのA0' と一致しませんでした

enumerate = \todos -> 
    setSGR [SetColor Foreground Vivid Red] >>= 
    (\_ -> putStrLn $ unlines $ map transform todos) >> 
    setSGR [Reset] 

を動作しません:

enumerate = \todos -> 
    setSGR [SetColor Foreground Vivid Red] >> 
    putStrLn . unlines . map transform todos >> 
    setSGR [Reset] 

を限り私はundestandすることができます、>>=は、その後のラムダ(\_ -> ...)で無視される変数を渡します。しかし、私はそれを>>を使って変換し、関数の合成をしても動作しないようです。

2つ目の違いがコンパイルされない原因は何ですか?なぜこれら2つの表現が同じでないのかを知ることは素晴らしいことです。

/Users/atimberlake/Webroot/HTodo/htodo/app/Main.hs:18:25: 
    Couldn't match expected type ‘IO a0’ with actual type ‘a1 -> IO()’ 
    In the second argument of ‘(>>)’, namely 
     ‘putStrLn . unlines . map transform todos’ 
    In the first argument of ‘(>>)’, namely 
     ‘setSGR [SetColor Foreground Vivid Red] 
     >> putStrLn . unlines . map transform todos’ 
    In the expression: 
     setSGR [SetColor Foreground Vivid Red] 
     >> putStrLn . unlines . map transform todos 
     >> setSGR [Reset] 

/Users/atimberlake/Webroot/HTodo/htodo/app/Main.hs:18:46: 
    Couldn't match expected type ‘a1 -> [String]’ 
       with actual type ‘[[Char]]’ 
    Possible cause: ‘map’ is applied to too many arguments 
    In the second argument of ‘(.)’, namely ‘map transform todos’ 
    In the second argument of ‘(.)’, namely 
     ‘unlines . map transform todos’ 
+0

は近くの投票に反対します。これは誤植ではなく、 '.'と' $ 'の仕組みの誤解です。 – amalloy

+0

合意しました...ハスケルで他の出発点として役立つと思います。実際にはわかったはずですが、ハスケルの複雑さに取り組むときは、これらのばかげた間違いをするのが一般的です。 – Wildhoney

答えて

2

これは動作します:

enumerate = \todos -> 
    setSGR [] >> 
    (putStrLn . unlines . map transform) todos >> 
    setSGR [] 

f . g . map h xsmap h xsあなたはg、その後fで構成されている機能であることを意味していること。しかし、map transform todosがリストであり、 は実際には関数putStrLn,unlinesおよびmap transformを構成してから、リストtodosに適用します。一般的に

f $ g $ h x = (f . g . h) x 

ので、作業の式:

putStrLn $ unlines $ map transform todos 

は同じです:

(putStrLn . unlines . map transform) todos 
+0

これで完璧な意味があります。ありがとうございました! – Wildhoney

関連する問題