2016-07-15 5 views
2

私はこれらの二つの式があります。

  1. foldr (-) 0 . map (uncurry (*)) $ coords 5 7

  2. foldr (-) 0 . map (uncurry (*)) (coords 5 7)

(1)作品のプリントアウト結果を、しかし、(2)はエラーが言う:

<interactive>:50:15: 
    Couldn't match expected type ‘a -> t0 c’ 
       with actual type ‘[Integer]’ 
    Relevant bindings include 
     it :: a -> c (bound at <interactive>:50:1) 
    Possible cause: ‘map’ is applied to too many arguments 
    In the second argument of ‘(.)’, namely 
     ‘map (uncurry (*)) (coords 5 7)’ 
    In the expression: foldr (-) 0 . map (uncurry (*)) (coords 5 7) 

これら2つの違いは何ですか?ありがとう。後者において

答えて

3

簡単に例があります:

Prelude> id . id $ "Example" 
"Example" 
Prelude> id . id ("Example") 
<interactive>:2:10: 
    Couldn't match expected type ‘a -> c’ with actual type ‘[Char]’ 
    Relevant bindings include it :: a -> c (bound at <interactive>:2:1) 
    In the first argument of ‘id’, namely ‘("Example")’ 
    In the second argument of ‘(.)’, namely ‘id ("Example")’ 
    In the expression: id . id ("Example") 

問題は関数適用が(.)よりも強く結合することです。 ($)修正このの固定性レベル:(...)とただし

id . id $ "Example" = (id . id) $ "Example" 
        = (id . id) "Example" 

、関数適用の勝利、あなたが2番目の引数として非機能付き(.)を使用して終了:

id . id ("Example") = id . id "Example" 
        = id . (id "Example") -- apply id 
        = id . ("Example") 
        = type error, since "Example" isn't a function 
+0

私は今、なぜ(uncurry(*)))を引数としてa()が問題を起こさなかったのか疑問に思っていますが、最後の引数(coords 5 7)はどうでしたか? btw、式に複数の$がある場合はどうなりますか?通訳者は表現をどのように解釈しますか? – linjunshi

3
foldr (-) 0 . map (uncurry (*)) $ coords 5 7 
-- is equivalent to 
(foldr (-) 0 . map (uncurry (*))) (coords 5 7) 
-- and to 
foldr (-) 0 (map (uncurry (*)) (coords 5 7)) 


foldr (-) 0 . map (uncurry (*)) (coords 5 7) 
-- is equivalent to 
foldr (-) 0 . (map (uncurry (*)) (coords 5 7)) 
-- and to 
\x -> foldr (-) 0 (map (uncurry (*)) (coords 5 7) x) 

map (uncurry (*)) (coords 5 7)の結果は、その2番目の引数として.に渡され、それはリストではなく、関数のため、型エラーが発生します。ある

注意もOKです:

foldr (-) 0 $ map (uncurry (*)) (coords 5 7) 
2

$は単純です中置形式のノーオペレーション。なぜなら、それは低い固定性と中置演算子です:それは括弧を持っていたかのように発生している任意の式が解析され

GHCi> :i $ 
($) :: (a -> b) -> a -> b -- Defined in ‘GHC.Base’ 
infixr 0 $ 

$.より低い固定性を持っているので、あなたの例では、

foldr (-) 0 . map (uncurry (*)) $ coords 5 7 

( (foldr (-) 0) 
    . (map (uncurry (*)))) 
$ (coords 5 7) 

として解析されます。これは、1 + 2 * 3とまったく同じ方法で動作します。*+よりも高い固定性を持つため、これは(1) + (2*3)として解析されます。あなたの場合には、これはfoldr (-) 0 . map (uncurry (*))です - - RHS式coords 5 7

$オペレータは、その後でそれがないすべてがLHSに関数を適用され、を評価しました。引数に関数を適用することはもちろん、ちょうどfunction (argument)を書いた場合にはどうなるでしょうか?しかし、正しい関数を指定する必要があります!あなたの試みに対し、

(foldr (-) 0 . map (uncurry (*))) (coords 5 7) 

として$せずに、あなたに例を書くために、あなたがしなければならないが、グループには、異なる解析されます。機能アプリケーションは、任意の中置よりも強くしても.結合し、ので、あなたの試みは

foldr (-) 0 . (map (uncurry (*)) (coords 5 7)) 
に相当します

これは意味をなさない。

関連する問題