Compare the haskell string head?

Struggling to learn Haskell how to take the head of a string and compare it with the next character until it finds a character that is true?

In pseudo code, I try:

while x == 'next char in string' add a new list to be returned

+4
source share
2 answers

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)

+4
source

There are two typical ways to get a row header. head and pattern matching (x:xs) .

In fact, the source for the head function is simply determined using pattern matching:

 head (x:_) = x head _ = badHead 

I highly recommend that you check the Haskell # Pattern match for you . He gives this example which can help:

 tell (x:y:[]) = "The list has two elements: " ++ show x ++ " and " ++ show y 

Note how the pattern is mapped to (x:y:[]) , that is, the list must have two elements and no more. To match the first two items in a longer list, simply replace [] with a variable (x:y:xs)

If you choose a pattern matching approach, you will need to use recursion.


Another approach is zip xs (drop 1 xs) . This little idiom creates tuples from neighboring pairs on your list.

 ghci> let xs = [1,2,3,4,5] ghci> zip xs (drop 1 xs) [(1,2),(2,3),(3,4),(4,5)] 

Then you can write a function that looks at these tuples one by one. It would also be recursive, but it could be written as foldl or foldr .


To understand recursion in Haskell, LYAH is again strongly recommended:

Learn about Haskell recursion #

+2
source

Source: https://habr.com/ru/post/1341838/


All Articles