lastLE cleanup
Remember lastLE? Back when we were trying to determine how many digits/bits it takes to represent a non-negative Integer, we used it to avoid copying a prefix of a list just to grab the value at its end.
lastLE :: Integer -> [Integer] -> Maybe (Integer, Int)
lastLE n xs =
let lastLE' xs prevVal prevIndex
| head xs <= n = lastLE' (tail xs) (head xs) (prevIndex + 1)
| otherwise = if prevIndex < 0 then Nothing
else Just (prevVal, prevIndex)
in lastLE' xs (-1) (-1)
It still bugs me. First, the mixed guards and if/else are less than idiomatic Haskell:
lastLE n xs =
let lastLE' xs prevVal prevIndex
| head xs <= n = lastLE' (tail xs) (head xs) (prevIndex + 1)
| prevIndex < 0 = Nothing
| otherwise = Just (prevVal, prevIndex)
in lastLE' xs (-1) (-1)
Second, the head and tail are clumsy:
lastLE n xs =
let lastLE' (x:xs) prevVal prevIndex
…
lastLE :: Integer -> [Integer] -> Maybe (Integer, Int)
lastLE n xs =
let lastLE' xs prevVal prevIndex
| head xs <= n = lastLE' (tail xs) (head xs) (prevIndex + 1)
| otherwise = if prevIndex < 0 then Nothing
else Just (prevVal, prevIndex)
in lastLE' xs (-1) (-1)
It still bugs me. First, the mixed guards and if/else are less than idiomatic Haskell:
lastLE n xs =
let lastLE' xs prevVal prevIndex
| head xs <= n = lastLE' (tail xs) (head xs) (prevIndex + 1)
| prevIndex < 0 = Nothing
| otherwise = Just (prevVal, prevIndex)
in lastLE' xs (-1) (-1)
Second, the head and tail are clumsy:
lastLE n xs =
let lastLE' (x:xs) prevVal prevIndex
…