`last (reverse [1..])`? Is there an infinite enumerator I could call last on?

Going through learn you some haskell I’m curious if there are mechanisms in haskell that would allow something like last (reverse [1..]) to return the expected value?

I tried a number of variations similar to

> foldl1 (\acc _ -> acc) (reverse [1..])
^CInterrupted

but to no avail.

I know

> foldr1 (\acc _ -> acc) (scanl1 (\acc _ -> acc) [1..])
1

and

> :type [1..]
[1..] :: (Num a, Enum a) => [a]

so is there some kind of reversed enumerator I could call last on e.g. [..1] where:

> :type [..1]
[..1] :: (Enum a, Num a) => [a]
> last [..1]
1

??

1 Like

Hello Kurt, I don’t think it is possible (at least not with Prelude’s list).

I have asked in #haskell-it to double-check, and the only suggestion to obtain this is “cheating” by abusing rewrite rules ({-# RULES …).

1 Like