Skip to main content
added 994 characters in body
Source Link
Unrelated String
  • 23k
  • 3
  • 32
  • 55

Haskell, 67 58 bytes

foldl1(#)
(a:b)#(x:y)|a==x=a:b#y
c#z|(1<$c)>(1<$z)=c|0<1=z

Attempt This Online!

-9 thanks to AnttiP mixing pattern failure and guard failure

Test harness truncates to run all the cases in one go, but it can be run infinitely.

Since the algorithm may be non-obvious and not everyone can read Haskell:

Explanation

foldl1 (#)

The function submission operating on a list of lists just reduces the list by a function of two lists, the infix operator #. (Since the input is guaranteed non-empty, foldl1 won't error.)

(a:b) # (x:y)
  | a == x = a : (b # y)

The critical part for handling duplicate infinite lists: if the first element of both lists is the same, then put that element in the output list and recur on the remainders of both.

c # z
  | (1 <$ c) > (1 <$ z) = c
  | otherwise = z

And here's the part that actually takes the longest list, if the previous clause failed. Since one of the lists can still be infinite, an actual length comparison is out of the question, but replacing every element of both lists with 1, a lexicographic comparison will terminate as soon as it reaches the point where one list ends and not the other.

Haskell, 67 58 bytes

foldl1(#)
(a:b)#(x:y)|a==x=a:b#y
c#z|(1<$c)>(1<$z)=c|0<1=z

Attempt This Online!

-9 thanks to AnttiP mixing pattern failure and guard failure

Test harness truncates to run all the cases in one go, but it can be run infinitely.

Haskell, 67 58 bytes

foldl1(#)
(a:b)#(x:y)|a==x=a:b#y
c#z|(1<$c)>(1<$z)=c|0<1=z

Attempt This Online!

-9 thanks to AnttiP mixing pattern failure and guard failure

Test harness truncates to run all the cases in one go, but it can be run infinitely.

Since the algorithm may be non-obvious and not everyone can read Haskell:

Explanation

foldl1 (#)

The function submission operating on a list of lists just reduces the list by a function of two lists, the infix operator #. (Since the input is guaranteed non-empty, foldl1 won't error.)

(a:b) # (x:y)
  | a == x = a : (b # y)

The critical part for handling duplicate infinite lists: if the first element of both lists is the same, then put that element in the output list and recur on the remainders of both.

c # z
  | (1 <$ c) > (1 <$ z) = c
  | otherwise = z

And here's the part that actually takes the longest list, if the previous clause failed. Since one of the lists can still be infinite, an actual length comparison is out of the question, but replacing every element of both lists with 1, a lexicographic comparison will terminate as soon as it reaches the point where one list ends and not the other.

added 59 characters in body
Source Link
Unrelated String
  • 23k
  • 3
  • 32
  • 55

Haskell, 6767 58 bytes

foldl1(#)
(a:b)#(x:y)|a==x=a:b#y|b#y
c#z|(1<$b1<$c)>(1<$y1<$z)=a:b|0<1=x:y
c#z=c++z=c|0<1=z

Attempt This Online!Attempt This Online!

-9 thanks to AnttiP mixing pattern failure and guard failure

Test harness truncates to run all the cases in one go, but it can be run infinitelycan be run infinitely.

Haskell, 67 bytes

foldl1(#)
(a:b)#(x:y)|a==x=a:b#y|(1<$b)>(1<$y)=a:b|0<1=x:y
c#z=c++z

Attempt This Online!

Test harness truncates to run all the cases in one go, but it can be run infinitely.

Haskell, 67 58 bytes

foldl1(#)
(a:b)#(x:y)|a==x=a:b#y
c#z|(1<$c)>(1<$z)=c|0<1=z

Attempt This Online!

-9 thanks to AnttiP mixing pattern failure and guard failure

Test harness truncates to run all the cases in one go, but it can be run infinitely.

Source Link
Unrelated String
  • 23k
  • 3
  • 32
  • 55

Haskell, 67 bytes

foldl1(#)
(a:b)#(x:y)|a==x=a:b#y|(1<$b)>(1<$y)=a:b|0<1=x:y
c#z=c++z

Attempt This Online!

Test harness truncates to run all the cases in one go, but it can be run infinitely.