Haskell Linked List Algebraic Datatype -
i attempting implement haskell algebraic datatype linked list (or perhaps more accurately linked list-like since don't know of way memory addressing using haskell) helper functions converting , haskell's naive list type , wrote following:
data linkedlist = nill | node (linkedlist a) deriving show hlisttolinkedlist :: [a] -> linkedlist hlisttolinkedlist [] = nill hlisttolinkedlist x:[] = node x nill hlisttolinkedlist x:xs = node (x) (stringtolinkedlist xs) linkedlisttohlist :: linkedlist char -> [char] linkedlisttohlist (node b) = ++ linkedlisttostring b linkedlisttohlist nill = ''
i following compiler error:
@5:1-5:21 parse error in pattern: hlisttolinkedlist
i'm not sure what's wrong function. please explain?
the minimal change needed make compile add parentheses patterns non-empty lists; e.g.
hlisttolinkedlist (x:xs) = ...
by requiring parentheses complex patterns, compiler need not know how many arguments each constructor takes; important trick reducing context sensitivity , promoting separate compilation.
Comments
Post a Comment