1
Axe / Re: Routines
« on: September 21, 2011, 02:15:09 am »
This is an example of some functional code that we wrote in one of my programming classes. The objective was to write a function that creates iterators. Each time you call the iterator, it returns one more than the previous call. Each time you call the iterator generator, it returns a new iterator. We do this with lambda syntax and closures. We wrote this in Scheme, but I was surprised and impressed to see it correctly execute in Axe.
The Axe code:
(I used liberal amounts of spaces to get it to line up the way I did)
Output:
An interesting side-effect of this is that the current iteration value is not stored in any variables - it's captured in the closure. Not that it's efficient to do this on a calculator with limited memory, but it's interesting to say the least.
The Axe code:
(I used liberal amounts of spaces to get it to line up the way I did)
Code: [Select]
.ITERATE
Iter()→L
Disp (L)()►Dec,i
Disp (L)()►Dec,i
Disp (L)()►Dec,i
Iter()→M
Disp (M)()►Dec,i
Disp (M)()►Dec,i
Disp (L)()►Dec,i
Lbl Iter
Return
λ(
λ(
λ(
r₁+1→r₁
)
)(0)
)()
Output:
Code: [Select]
asm(prgmITERATE
1
2
3
1
2
4
An interesting side-effect of this is that the current iteration value is not stored in any variables - it's captured in the closure. Not that it's efficient to do this on a calculator with limited memory, but it's interesting to say the least.