나는 하스켈 (현재 패턴 매칭 학습)에서 초보자 해요 작동하지 않습니다 벡터를 정의 기능과 I는 벡터 정의에 대한 간단한 함수를 작성하려고 :하스켈
vector :: (Num a) => a -> a -> String
vector 0 0 = "This vector has 0 magnitude."
vector x y = "This vector has a magnitude of " ++ show(sqrt(x^2 + y^2)) ++ "."
을하지만 오류의 무리를 얻을 수있는 나는 전혀 이해하지 못한다.
helloworld.hs:9:8: error:
• Could not deduce (Eq a) arising from the literal ‘0’
from the context: Num a
bound by the type signature for:
vector :: Num a => a -> a -> String
at helloworld.hs:8:1-37
Possible fix:
add (Eq a) to the context of
the type signature for:
vector :: Num a => a -> a -> String
• In the pattern: 0
In an equation for ‘vector’:
vector 0 0 = "This vector has 0 magnitude."
helloworld.hs:10:51: error:
• Could not deduce (Show a) arising from a use of ‘show’
from the context: Num a
bound by the type signature for:
vector :: Num a => a -> a -> String
at helloworld.hs:8:1-37
Possible fix:
add (Show a) to the context of
the type signature for:
vector :: Num a => a -> a -> String
• In the first argument of ‘(++)’, namely
‘show (sqrt (x^2 + y^2))’
In the second argument of ‘(++)’, namely
‘show (sqrt (x^2 + y^2)) ++ "."’
In the expression:
"This vector has a magnitude of "
++ show (sqrt (x^2 + y^2)) ++ "."
helloworld.hs:10:56: error:
• Could not deduce (Floating a) arising from a use of ‘sqrt’
from the context: Num a
bound by the type signature for:
vector :: Num a => a -> a -> String
at helloworld.hs:8:1-37
Possible fix:
add (Floating a) to the context of
the type signature for:
vector :: Num a => a -> a -> String
• In the first argument of ‘show’, namely ‘(sqrt (x^2 + y^2))’
In the first argument of ‘(++)’, namely
‘show (sqrt (x^2 + y^2))’
In the second argument of ‘(++)’, namely
‘show (sqrt (x^2 + y^2)) ++ "."’
Failed, modules loaded: none.
Prelude> :load helloworld
[1 of 1] Compiling Main (helloworld.hs, interpreted)
helloworld.hs:10:51: error:
• Could not deduce (Show a) arising from a use of ‘show’
from the context: Integral a
bound by the type signature for:
vector :: Integral a => a -> a -> String
at helloworld.hs:8:1-42
Possible fix:
add (Show a) to the context of
the type signature for:
vector :: Integral a => a -> a -> String
• In the first argument of ‘(++)’, namely
‘show (sqrt (x^2 + y^2))’
In the second argument of ‘(++)’, namely
‘show (sqrt (x^2 + y^2)) ++ "."’
In the expression:
"This vector has a magnitude of "
++ show (sqrt (x^2 + y^2)) ++ "."
helloworld.hs:10:56: error:
• Could not deduce (Floating a) arising from a use of ‘sqrt’
from the context: Integral a
bound by the type signature for:
vector :: Integral a => a -> a -> String
at helloworld.hs:8:1-42
Possible fix:
add (Floating a) to the context of
the type signature for:
vector :: Integral a => a -> a -> String
• In the first argument of ‘show’, namely ‘(sqrt (x^2 + y^2))’
In the first argument of ‘(++)’, namely
‘show (sqrt (x^2 + y^2))’
In the second argument of ‘(++)’, namely
‘show (sqrt (x^2 + y^2)) ++ "."’
Failed, modules loaded: none.
누군가가 어떻게 제대로이 기능을 작성하는 나를 설명 할 수, 적어도 무슨 일이 vector 0 0
잘못?
힌트 :': t sqrt'와': t show'. – Jubobs
가능한 오류 수정 방법을 시도 했습니까? –