A general approach would be to create a function that recursively evaluates the start line until it finds a false value or reaches the end.
To do this, you need to
- understand recursion (precondition: understand recursion) and how to write recursive functions in Haskell
- know how to use the head function
- it's possible to know how to use list comprehension in Haskell
I have notes on Haskell that may be useful, but you can find Another Haskell tutorial more detailed (sections 3.3. Lists, 3.5 Functions and 7.8. Additional lists are likely to be good places to address marked items).
EDIT0: An example of using guards to check a head element and continue only if it matches the second element:
someFun :: String -> String someFun[] = [] someFun [x:y:xs] | x == y = someFun(y:xs) | otherwise = []
EDIT1:
I kind of want to say x = (newlist), and then, not otherwise = [] otherwise = [newlist], if that makes sense? This makes sense in an imperative programming paradigm (e.g. C or Java), especially for functional approaches.
Here is a concrete example, I hope to emphasize the difference between the concept of if, then else, which the quote offers, and what happens in the SomeFun function:
When we call SomeFun [a,a,b,b] , we map it to SomeFun [x:y:xs] , and since x is 'a' and y is 'a' and x==y , then SomeFun [a,a,b,b] = SomeFun [a,b,b] , which again coincides with SomeFun [x:y:xs] but the condition x==y is false, so we use a protective shell otherwise, and therefore we get SomeFun [a,a,b,b] = SomeFun [a,b,b] = [] . Therefore, the result of SomeFun [a,a,b,b] is [] .
So where did the data go? Ok, I will raise my hands and admit a bug in the code, which is now a function that I use to explain how the Haskell functions work.
I find it helpful to think more about constructing mathematical expressions rather than programming. Thus, the expression to the right of = is your result, not the assignment in the imperative (for example, Java or C sense).
I hope that a specific example shows that Haskell evaluates expressions using substitution, so if you don't want something in your result then don't include it in that expression. And vice versa, if you want something as a result, then put it in the expression.
Since your psuedo code
while x == 'next char in string' add a new list to be returned
I will modify the SomeFun function to do the opposite, and let me know how to modify it to work as I wish.
someFun2 :: String -> String someFun2[] = [] someFun2 [x:y:xs] | x == y = [] | otherwise = x : someFun(y:xs)
Result:
SomeFun2 [a,a,b,b] = []SomeFun2 [a,b,b,a,b] = [a]SomeFun2 [a,b,a,b,b,a,b] = [a,b,a]SomeFun2 [a,b,a,b] = [a,b,a,b]
(I would like to add at this point that these various code snippets have not been verified since I do not have a compiler, so please indicate any errors so that I can fix them, thanks)