i new in comunity. learn haskell , have difficulties haskell-coding. hope can me.
i searched here , in google, without success.
my problem ist fowllows: want write function takes list parameter this:
mystringlistfilter :: [string] -> [string] process following steps:
remove first letter
mystringlistfilter mylist = map tail strliste mylistfilter every element in list begins "u" or "u".
mystringlistfilter mylist = filter (´elem´ ['u', 'u']) (map tail strliste mylist)
step 2 doesn't work. error.
how achieve solution, if want following:
input: ["butter", "chees", "aubergine", "egg", "milk", "bubble", "curry"] output: ["chees", "egg", "milk"]
the type of filter is
filter :: (a -> bool) -> [a] -> [a] so if want filter list of strings according predicate, need function string -> bool, wrote, (`elem` ['u',u']) has type char -> bool.
so need function
beginswithu :: string -> bool the easiest way define is
beginswithu (c:_) = c == 'u' || c == 'u' beginswithu _ = false -- empty list then have misunderstood how filter works, keeps elements satisfying predicate, want remove them, need compose predicate not (or define doesn'tbeginwithu directly).
however, 7stud points out, not want change elements want keep original list, what
mystringlistfilter mylist = filter (not . beginswithu) (map tail mylist) or, point-free:
mystringlistfilter = filter (not . beginswithu) . map tail would achieve. need incorporate tail predicate too, , need no map, yield
mystringlistfilter = filter (not . beginswithu . tail) or, if possibility empty string occurs in input list shall dealt benignly,
mystringlistfilter = filter (not . beginswith . drop 1) since tail "" produce *** exception: prelude.tail: empty list whereas drop 1 "" produces "".
but, want keep original list element, can define predicate directly @ second character,
secondcharisu :: string -> bool secondcharisu (_:c:_) = c == 'u' || c == 'u' secondcharisu _ = false mystringlistfilter = filter (not . secondcharisu)
Comments
Post a Comment