私は文字列を取ってDirection
のリストにしたいと思います。たとえば、"UDDUD"
は[U,D,D,U,D]
を返し、U
またはD
を含まない文字列はNothing
を返します(たとえば、「UDYD」はNothing
を返します)。ハスケル - 多分と再帰
data Direction = U | D
deriving (Show, Eq)
-- where U is Up and D is Down
findDirection :: [Char] -> Maybe [Direction]
findDirection [] = Nothing
findDirection ['U'] = Just [U]
findDirection ['D'] = Just [D]
findDirection (x:xs)
| x == 'U' = Just (U : findDirection xs)
| x == 'D' = Just (D : findDirection xs)
| otherwise = Nothing
私は次のエラーを取得する:
Couldn't match expected type ‘[Direction]’
with actual type ‘Maybe [Direction]’
In the second argument of ‘(:)’, namely ‘findDirection xs’
In the first argument of ‘Just’, namely
‘(U : findDirection xs)’
Test_findDirection.hs:8:32:
Couldn't match expected type ‘[Direction]’
with actual type ‘Maybe [Direction]’
In the second argument of ‘(:)’, namely ‘findDirection xs’
In the first argument of ‘Just’, namely
‘(D : findDirection xs)’
私はそれを理解し、Just (D : findDirection xs)
とJust (U : findDirection xs)
はタイプ[Direction]
のですか。これはなぜですか?私は何を間違えているのですか?
'U:findirectionのxs'(と' D:findDirection xs' )はタイプミスです:あなたは 'Direction'を'たぶん[Direction] 'に賛成できません。また、あなたは次のように書いています:* [...] 'U'や' D'を含まない文字列は 'Nothing'を返します(例えば' 'UDYD''は' Nothing'を返します)。*あなたは自分自身と矛盾していませんか? ? – Jubobs