Identifying a specific index in a list of strings and removing everything in the list beyond that index in Haskell -
i've started learning haskell, , i've encountered problem can't seem overcome.
my input list of strings contain 3 characters each. want split list @ point , have output list until point.
example:
main> listsplitter ["aaa", "bbb", "ccc", "stp", "ddd"] main> ["aaa", "bbb", "ccc"] main> listsplitter ["aaa", "stp", "bbb", "ccc", "ddd"] main> ["aaa"]
where stp in case identifying string @ stop output string.
if it's possible split string in similar way, or perhaps easier, useful. list of strings further problem i'm working on string. this:
main> stringsplitter "aaabbbcccstpddd main> "aaabbbccc" main> stringsplitter "aaastpbbbcccddd" main> "aaa"
i should mention achieve without importing data modules.
thanks time!
the solution first question has 2 parts:
- finding
takewhile
function - writing predicate use
takewhile
, once you've found it
for (1), try searching hoogle [a] -> [a]
, not full type signature want part of it, , scrolling through matches; find takewhile
before long.
for (2) want involving (==)
or perhaps (/=)
. playing around bit may come with:
keep :: string -> bool keep x = x /= "stp"
and can simplify using partial application instead:
keep :: string -> bool keep = (/= "stp")
finally combine parts (1) , (2):
listsplitter :: [string] -> [string] listsplitter = takewhile (/= "stp")
to solve second problem, of stringsplitter
, can't use takewhile
, because can @ 1 item @ time, , need @ more. there elegant way using data.list
and/or data.list.split
(exercise: find elegant way), quite easy implement recursively pattern matching starting head of list:
stringsplitter :: string -> string stringsplitter [] = [] stringsplitter ('s':'t':'p':_) = [] stringsplitter (x:xs) = x : stringsplitter xs
Comments
Post a Comment