7
(용서하십시오 끔찍하게 인위적인 예)"where"절에서 표현식 제네릭 형식을 어떻게 제공합니까?
는 내가하고 싶은 것은 어디 절에서 유형을 지정할 수 있습니다 :
somemap :: (a -> b) -> [a] -> [b]
somemap f xs = ys
where
some = take 5 xs :: [a]
ys = map f some :: [b]
을하지만이 오류가 발생합니다
*Main> :load file.hs
[1 of 1] Compiling Main (file.hs, interpreted)
fil.hs:15:18:
Couldn't match expected type `a1' against inferred type `a'
`a1' is a rigid type variable bound by
an expression type signature at file.hs:15:25
`a' is a rigid type variable bound by
the type signature for `somemap' at file.hs:12:12
Expected type: [a1]
Inferred type: [a]
In the second argument of `take', namely `xs'
In the expression: take 5 xs :: [a]
file.hs:16:13:
Couldn't match expected type `b1' against inferred type `b'
`b1' is a rigid type variable bound by
an expression type signature at file.hs:16:24
`b' is a rigid type variable bound by
the type signature for `somemap' at file.hs:12:17
In the first argument of `map', namely `f'
In the expression: map f some :: [b]
In the definition of `ys': ys = map f some :: [b]
Failed, modules loaded: none.
을
구체적인 유형을 지정하는 경우 a
및을 Int
으로 대체하십시오. b
에 대한 335,453,210은 아무 문제 : 그래서 제 질문은
somemap :: (Int -> Bool) -> [Int] -> [Bool]
somemap f xs = ys
where
some = take 5 xs :: [Int]
ys = map f some :: [Bool]
없습니다
: 어떻게 일반적인 유형을 지정하고 어디 절에 제약을 입력합니까?where
절 내부
http://stackoverflow.com/questions/7408911/haskell-magical-code-whats-going-on-here의 코드는 그러한 "추악한 도우미 기능"에서 발생할 수있는 혼란의 좋은 예입니다. –