开发者

Compare the head of a haskell string?

Struggling to learn Haskell, how does one take the head of a string and compare it with the next character untill it finds a character thats note true?

In pseudo code I'm trying to:

while x == 'ne开发者_JAVA百科xt char in string' put in new list to be returned


The general approach would be to create a function that recursively evaluates the head of the string until it finds the false value or reaches the end.

To do that, you would need to

  • understand recursion (prerequisite: understand recursion) and how to write recursive functions in Haskell
  • know how to use the head function
  • quite possibly know how to use list comprehension in Haskell

I have notes on Haskell that you may find useful, but you may well find Yet Another Haskell Tutorial more comprehensive (Sections 3.3 Lists; 3.5 Functions; and 7.8 More Lists would probably be good places to start in order to address the bullet points I mention)

EDIT0: An example using guards to test the head element and continue only if it the same as the second element:

someFun :: String -> String 
someFun[] = [] 
someFun [x:y:xs]
    | x == y = someFun(y:xs)
    | otherwise = []

EDIT1:

I sort of want to say x = (newlist) and then rather than otherwise = [] have otherwise = [newlist] if that makes any sense? It makes sense in an imperative programming paradigm (e.g. C or Java), less so for functional approaches

Here is a concrete example to, hopefully, highlight the different between the if,then, else concept the quote suggests and what is happening in the SomeFun function:

When we call SomeFun [a,a,b,b] we match this 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 matches SomeFun [x:y:xs] but condition x==y is false, so we use the otherwise guard, and so we get SomeFun [a,a,b,b] = SomeFun [a,b,b] = []. Hence, the result of SomeFun [a,a,b,b] is [].

So where did the data go? .Well, I'll hold my hands up and admit a bug in the code, which is now a feature I'm using to explain how Haskell functions work.

I find it helpful to think more in terms of constructing mathematical expressions rather than programming operations. So, the expression on the right of the = is your result, and not an assignment in the imperative (e.g. Java or C sense).

I hope the concrete example has shown that Haskell evaluates expressions using substitution, so if you don't want something in your result, then don't include it in that expression. Conversely, if you do want something in the result, then put it in the expression.

Since your psuedo code is

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

I'll modify the SomeFun function to do the opposite and let you figure out how it needs to be modified to work as you desire.

someFun2 :: String -> String 
someFun2[] = [] 
someFun2 [x:y:xs]
    | x == y = []
    | otherwise = x : someFun(y:xs)

Example Output:

  • 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'd like to add at this point, that these various code snippets aren't tested as I don't have a compiler to hand, so please point out any errors so I can fix them, thanks)


There are two typical ways to get the head of a string. head, and pattern matching (x:xs).

In fact, the source for the head function shows is simply defined with pattern matching:

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

I highly recommend you check out Learn You a Haskell # Pattern Matching. It gives this example, which might help:

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

Notice how it pattern matched against (x:y:[]), meaning the list must have two elements, and no more. To match the first two elements in a longer list, just swap [] for a variable (x:y:xs)

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


Another approach is the zip xs (drop 1 xs). This little idiom creates tuples from adjacent pairs in your list.

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

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


For understanding recursion in Haskell, LYAH is again highly recommended:

Learn You a Haskell # Recursion

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜