|
|
|
|
|
by jfoutz
3791 days ago
|
|
the prelude implementation is foldMap on list. as i understand it, a cons cell will be created with the head pointer pointing at 1, and the tail at a lazily evaluated thunk. + tries to evaluate, which creates a cons cell pointing at 2, and a thunk. 1 and 2 are added. so we've got two cells hanging around, but no references to the first one, so the gc can grab it whenever. it'll just kind of chug through the list. it's kinda wasteful to keep making the cells, but it's easy to read. shrug however ghc is very smart. it might be clever enough to optimize away the cells. it might also do immediate ints, rather than pointers to '1', i'm pretty hazy on when there's an int, and when there's a bigint. |
|
you can see
where it compiles to a recursive loop with an accumulatortypicall the [1..100] gets compiled to (enumFromTo 1 100) and it can be desugared further..
so, yeah, foldmap compiles to foldr which may compile down further