1
putStr (printf "abc%d\n" 3)
이 스크립트로 실행될 때 3
이 모호한 이유는 무엇입니까? 아니요. 012h로 실행 중일 때이 모호합니까? 즉, 왜 스크립트에서 3
의 유형을 선언해야하지만 ghci는 선언하지 않아야합니까? 여기 스크립트에 대해 printf (putStr과 함께 사용하는 경우)에서 숫자 유형을 선언해야하지만 ghci에서는 그렇지 않아야하는 이유는 무엇입니까?
ghci
내에 동작이다
여기
$ cat runmain-good
#!/usr/bin/env runghc
import Text.Printf
main = putStr (printf "abc%d\n" (3 :: Int))
$ ./runmain-good
abc3
가 3
의 형태 인 경우와 script
의 오동작이다 Int
로 선언 할 때
$ ghci
GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> putStr "abc3\n"
abc3
Prelude> import Text.Printf
Prelude Text.Printf> printf "abc%d\n" 3
abc3
Prelude Text.Printf> let main = putStr (printf "abc%d\n" 3)
Prelude Text.Printf> main
abc3
Prelude Text.Printf> let main = printf "abc%d\n" 3 :: String
Prelude Text.Printf> main
"abc3\n"
Prelude Text.Printf> :quit
Leaving GHCi.
가 script
의 올바른 동작이다 모호한 ... 일반적인 사용자 친화적 인 하스켈 오류 :
$ cat runmain-bad
#!/usr/bin/env runghc
import Text.Printf
main = putStr (printf "abc%d\n" 3)
$ ./runmain-bad
runmain-bad:3:16:
No instance for (PrintfArg a0) arising from a use of `printf'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance [safe] PrintfArg Char -- Defined in `Text.Printf'
instance [safe] PrintfArg Double -- Defined in `Text.Printf'
instance [safe] PrintfArg Float -- Defined in `Text.Printf'
...plus 12 others
In the first argument of `putStr', namely `(printf "abc%d" 3)'
In the expression: putStr (printf "abc%d" 3)
In an equation for `main': main = putStr (printf "abc%d" 3)
runmain-bad:3:33:
No instance for (Num a0) arising from the literal `3'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Num Double -- Defined in `GHC.Float'
instance Num Float -- Defined in `GHC.Float'
instance Integral a => Num (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
...plus 11 others
In the second argument of `printf', namely `3'
In the first argument of `putStr', namely `(printf "abc%d" 3)'
In the expression: putStr (printf "abc%d" 3)
putStr이 필요하지 않습니다. – augustss
@augustss 감사합니다. 나는 이것을 깨닫지 못했다. 방금 너의 팁을 시험해 봤어. 물론'putStr'을 삭제해도 _literal 3 ambiguous_ 오류는 제거되지 않습니다. 그러나 이제는 올바른 표현이'main = printf "abc % d \ n"(3 :: Int)'로 감소합니다. ... 하스켈의 수학적 간결함은 알고리즘의 본질을 비추는 데 도움이되기 때문에 가장 아름다운 특징 중 하나입니다. 특히이 상황에서 숫자가 명확 해지지는 않지만 Hindley-Milner를 강력하게 사용하면 더욱 좋습니다. –