Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - WaeV

Pages: [1]
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)
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.

2
Axe / Re: [Tutorial] Edition 2: Using Axe to its full functional form λ
« on: September 21, 2011, 02:11:02 am »
Yeah sure!  You can do that. The outer lambda returns the inner lambda - your phrase "iterator generator" is a good one - but it could return any lambda.

Edit: Here's a version which lets you decide where to start.
The Axe code:
Code: [Select]
.ITERATE

Iter(10)→L

Disp (L)()►Dec,i
Disp (L)()►Dec,i
Disp (L)()►Dec,i

Lbl Iter
Return
λ(
  λ(
    λ(
      r₁+1→r₁
    )
  )(r₁)
)()

Output:
Code: [Select]
asm(prgmITERATE
    11
    12
    13

3
Axe / Re: [Tutorial] Edition 2: Using Axe to its full functional form λ
« on: September 19, 2011, 09:33:20 pm »
Hey guys, I was really impressed by these new features of Axe, so I decided to register to make this post!  I figured we could use a non-trivial example of how lambdas can be actually used.


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.  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)
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 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...

Pages: [1]