I wrote this Racket code to find and display the longest sublist in a list of lists, but if the lengths of multiple sublists are equal and all the longest, I want it to return the last sublist having the longest length.
(define longest '())
;returns longest sublist in a list of lists
(define (longestSub losl)
(set! longest (car losl))
(for ([x (- (length losl) 1)])
(if (>= (length (list-ref losl x)) (length longest))
(set! longest (list-ref losl x))
(void losl)))
(display longest))
For example, if "losl" is ((1 2) (3 4 5) (6 7 8)), I want it to return (6 7 8), but right now it would return (3 4 5). Can anyone tell me what I'm doing wrong?