在源文件中:
\naddV1 x y = (if x > y then y else x) + 5\naddV2 = \\x -> \\y -> (if x > y then y else x) + 5\nRun Code Online (Sandbox Code Playgroud)\n在 ghci 中:
\nghci> :l h.hs\nghci> addV1 4.5 10\n\n<interactive>:1083:1: warning: [-Wtype-defaults]\n \xe2\x80\xa2 Defaulting the following constraints to type \xe2\x80\x98Double\xe2\x80\x99\n (Show a0) arising from a use of \xe2\x80\x98print\xe2\x80\x99 at <interactive>:1083:1-12\n (Ord a0) arising from a use of \xe2\x80\x98it\xe2\x80\x99 at <interactive>:1083:1-12\n (Fractional a0)\n arising from a use of \xe2\x80\x98it\xe2\x80\x99 at <interactive>:1083:1-12\n \xe2\x80\xa2 In a stmt of an interactive GHCi command: print it\n9.5\nghci> addV2 4.5 10\n\n<interactive>:1084:7: error:\n \xe2\x80\xa2 No instance for (Fractional Integer)\n arising from the literal \xe2\x80\x984.5\xe2\x80\x99\n \xe2\x80\xa2 In the first argument of \xe2\x80\x98addV2\xe2\x80\x99, namely \xe2\x80\x984.5\xe2\x80\x99\n In the expression: addV2 4.5 10\n In an equation for \xe2\x80\x98it\xe2\x80\x99: it = addV2 4.5 10\nRun Code Online (Sandbox Code Playgroud)\n当我加载源文件时,我确实收到以下警告:
\nh.hs:10:1: warning: [-Wmissing-signatures]\n Top-level binding with no type signature:\n addV1 :: (Num a, Ord a) => a -> a -> a\n |\n10 | addV1 x y = (if x > y then y else x) + 5\n | ^^^^^\n\nh.hs:11:1: warning: [-Wmissing-signatures]\n Top-level binding with no type signature:\n addV2 :: Integer -> Integer -> Integer\n |\n11 | addV2 = \\x -> \\y -> (if x > y then y else x) + 5\n | ^^^^^\n\nh.hs:11:46: warning: [-Wtype-defaults]\n \xe2\x80\xa2 Defaulting the following constraints to type \xe2\x80\x98Integer\xe2\x80\x99\n (Num a0) arising from a use of \xe2\x80\x98+\xe2\x80\x99 at h.hs:11:46\n (Ord a0) arising from a use of \xe2\x80\x98>\xe2\x80\x99 at h.hs:11:27\n \xe2\x80\xa2 In the expression: (if x > y then y else x) + 5\n In the expression: \\ y -> (if x > y then y else x) + 5\n In the expression: \\ x -> \\ y -> (if x > y then y else x) + 5\n |\n11 | addV2 = \\x -> \\y -> (if x > y then y else x) + 5\n | ^\nOk, one module loaded.\nRun Code Online (Sandbox Code Playgroud)\n非常感谢!
\n编辑:\n似乎当从源文件加载这些函数时,haskell 决定第二个函数应默认为 Integer。但是当我在 ghci 命令行定义这两个函数时,它们都具有相同的类型:
\nghci> addV1 x y = (if x > y then y else x) + 5\n\n<interactive>:1093:1: warning: [-Wname-shadowing]\n This binding for \xe2\x80\x98addV1\xe2\x80\x99 shadows the existing binding\n defined at h.hs:2:1\nghci> :t addV1\naddV1 :: (Num a, Ord a) => a -> a -> a\nghci> addV2 = \\x -> \\y -> (if x > y then y else x) + 5\n\n<interactive>:1095:1: warning: [-Wname-shadowing]\n This binding for \xe2\x80\x98addV2\xe2\x80\x99 shadows the existing binding\n defined at h.hs:3:1\nghci> :t addV2\naddV2 :: (Num a, Ord a) => a -> a -> a\nRun Code Online (Sandbox Code Playgroud)\n编辑:\n当我将类型签名添加到源文件时,两个函数都执行良好:
\naddV2 :: (Num a, Ord a) => a -> a -> a\naddV2 = \\x -> \\y -> (if x > y then y else x) + 5\nRun Code Online (Sandbox Code Playgroud)\n我刚刚开始使用 haskell。我想我在这里学到的教训是,始终提供显式类型声明很重要,这正是我计划做的事情(只是做书上的练习)。
\n| 归档时间: |
|
| 查看次数: |
78 次 |
| 最近记录: |