Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does `until` work?

Tags:

haskell

I wanted to answer to this question using until. But that does not work and I've concluded that I don't understand until.

So I take the function given by the OP, verbatim:

removeAdjDups :: (Eq a) => [a] -> [a]
removeAdjDups []           =  []
removeAdjDups [x]          =  [x]
removeAdjDups (x : y : ys)
  | x == y = removeAdjDups ys
  | otherwise = x : removeAdjDups (y : ys)

Then I write a True/False function retuning whether there is a duplicate:

hasAdjDups :: (Eq a) => [a] -> Bool
hasAdjDups []           =  False
hasAdjDups [x]          =  False
hasAdjDups (x : y : ys)
  | x == y = True
  | otherwise = hasAdjDups (y : ys)

Finally I use until as follows:

f :: (Eq a) => [a] -> [a]
f x = until hasAdjDups removeAdjDups x

And that does not work :

> hasAdjDups  "aabccddcceef"
True
> removeAdjDups   "aabccddcceef"
"bf"
> f "aabccddcceef"
"aabccddcceef"

I misunderstand until, or I did a mistake ?

like image 636
Stéphane Laurent Avatar asked Mar 22 '18 08:03

Stéphane Laurent


People also ask

What is the rule of until?

Until indicates when something will happen, begin, or end. Till means the same thing as until. Till is not an abbreviation of until—it's actually older than until—and it should not be written with an apostrophe.

Should I use till or until?

Until, till, and 'til are all used in modern English to denote when something will happen. Until and till are both standard, but what might be surprising is that till is the older word. 'Til, with one L, is an informal and poetic shortening of until. The form 'till, with an additional L, is rarely if ever used today.

What is a meaning of until?

Definition of until (Entry 1 of 2) 1 chiefly Scotland : to. 2 —used as a function word to indicate continuance (as of an action or condition) to a specified time stayed until morning. 3 : before sense 2 not available until tomorrow we don't open until ten. until.

Which tense is used with until?

Remember, "until" cannot be used followed by the future tense, all the others alternatives are suitable for until, past tense, past continuous, present tense, present continuous, present perfect..

What does the “do until” function do in a flow?

The screenshot of my flow as below: The first value of the “Do until” would fill in the “status Value” dynamic content of the “Get item” action.

How do you use the word until in a sentence?

An online dictionary (7) defines “until” as a preposition or conjunction that means primarily two things: “up to the time of” and “before (a specified time).” It gives examples such as “We danced until dawn” and “She can’t leave until Friday.” In the dancing sentence, the people stopped dancing when the sun began to rise.

What is the meaning of “DO UNTIL LOOP”?

As the words itself says that to do some task until a criterion is reached, Do until the loop is used in almost all of the programming languages, in VBA also we sometimes use Do until loop. Do Until Loop means to do something until the condition becomes TRUE. It is like a logical function works based on TRUE or FALSE.

Is it “till” or “until”?

So you probably don’t want to use that if you mean “until”! Mignon Fogarty is the founder the Quick and Dirty Tips podcast network, the creator of the Grammar Girl website and Grammar Girl podcast with Grammar Girl Mignon Fogarty. Now we’ve come to the word “till.”


1 Answers

The until :: (a -> Bool) -> (a -> a) -> a -> a is documented as:

until p f yields the result of applying f until p holds.

It is implemented like:

until p f = go
  where
    go x | p x          = x
         | otherwise    = go (f x)

So you provide a predicate p, and a function f. The function is also given an initial value x. By using recursion, it first checks if p x holds. In case it does, it returns x, otherwise, it makes a recursive call with f x as the new x.

So a more clean (but less efficient) implementation is probably:

until p f x | p x = x
            | otherwise = until p f (f x)

If we analyze your function, we see:

f x = until hasAdjDups removeAdjDups x

So that means f will terminale removing adjacent duplicate characters from the moment it has adjacent duplicate characters. You probably want the opposite predicate:

f x = until (not . hasAdjDups) removeAdjDups x

Or even shorter:

f = until (not . hasAdjDups) removeAdjDups
like image 103
Willem Van Onsem Avatar answered Oct 14 '22 13:10

Willem Van Onsem