### lastLE cleanup

Remember

It still bugs me. First, the mixed guards and if/else are less than idiomatic Haskell:

Second, the

Third, that accumulator should be strict, so pretend there's a

I think I'd rather see

Come to think of it, we can be consistent in our use of pairs:

Now, one more thing. Can I get rid of the magic numbers there, the -1s? The -1 as initial

would be the way to go. Hey! We never even use that value here--it's used in the caller--so we can write

and it should work (sure enough, it does), as well as expressing explicitly that we don't use it. Well, that's one magic number down, at least.

**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****| x <= n = lastLE' xs x (prevIndex + 1)****| prevIndex < 0 = Nothing****| otherwise =****Just (prevVal, prevIndex)****in lastLE' xs (-1) (-1)**Third, that accumulator should be strict, so pretend there's a

**!**in front of**prevIndex**in that**let**.I think I'd rather see

**Nothing**as the**otherwise**case.**lastLE n xs =****let lastLE' (x:xs) prevVal !prevIndex****| x <= n = lastLE' xs x (prevIndex + 1)****| prevIndex >= 0 =****Just (prevVal, prevIndex)****| otherwise = Nothing****in lastLE' xs (-1) (-1)**Come to think of it, we can be consistent in our use of pairs:

**lastLE n xs =****let lastLE' (x:xs) !prev@(val, index)****| x <= n = lastLE' xs (x, index + 1)****| prevIndex >= 0 =****Just prev****| otherwise = Nothing****in lastLE' xs (-1, -1)**Now, one more thing. Can I get rid of the magic numbers there, the -1s? The -1 as initial

**index**needs to be there to get the correct position in the list of powers of the base. The initial**val**will never be used. If the whole pair is strict, though, we couldn't pass in**⊥**("bottom", the undefined value, which is**undefined**in Haskell). Perhaps**lastLE n xs =****let lastLE' (x:xs) prev@(val, !index)****| x <= n = lastLE' xs (x, index + 1)****| index >= 0 =****Just prev****| otherwise = Nothing****in lastLE' xs (undefined, -1)**would be the way to go. Hey! We never even use that value here--it's used in the caller--so we can write

**lastLE n xs =****let lastLE' (x:xs) prev@(_, !index)****| x <= n = lastLE' xs (x, index + 1)****| index >= 0 =****Just prev****| otherwise = Nothing****in lastLE' xs (undefined, -1)**and it should work (sure enough, it does), as well as expressing explicitly that we don't use it. Well, that's one magic number down, at least.

## Comments