Learning Haskell: confusion with reverse function and recursion -
i've started learn haskell , trying write simple function takes list of strings , reverses each string in list:
revcomp :: [string] -> [string] revcomp [] = [] revcomp [x] = [] ++ [reverse x] revcomp (x:xs) = revcomp [xs]
when try load code in ghci, error:
couldn't match expected type `char' actual type `[char]' expected type: string actual type: [string]
could explain , problem is? much.
the first 3 lines fine. type signature correct, second line correct, , third. (however, [] ++ [reverse x]
same [reverse x]
.)
the fourth line, however, wrong. not not use x
@ on right-hand side, have type error: revcomp [xs]
calls revcomp
single-element list has xs
element. here, x
first element of list, , xs
rest of list. so, since xs
has type [string]
, [xs]
has type [[string]]
, revcomp
takes [string]
! want reverse x
, , prepend result of reversing rest of list.
you can use revcomp xs
reverse each string in rest of list, , (:)
prepend value list (with same syntax seen in x:xs
pattern used on left-hand side). should enough information fix last line. makes third line redundant, way, since [x]
x:[]
.
Comments
Post a Comment