Lbl CAT
r1+r2 -> A
Return
CAT(5,6)
5 + 6 -> A
HL is like the Ans of Axe.
Lbl CAT
r1+r2
Return
CAT(5,6)+A -> B
5 + 6 -> A + A ->B
5 + 6 + A -> B
def CAT(arg1, arg2)
return arg1+arg2
end
B = A + CAT(5, 6)
(ADDRESS)(ARGUMENTS)
(LvCAT)(5,6)
.Same as:
CAT(5,6)
Copy(LvCAT,LvDOG,LvCATE-LvCAT)
Disp LvCATE-LvCAT >Dec
λ(r1+r2)(5,6)
λ(r1+r2) -> L
(L)(5,6)
L = lambda { | arg1, arg2 | arg1+arg2 }
L.call(5,6) #returns 11
Lbl CAT
r1+r2->r4
DOG(r1,r2,r3,r4)
Goto DOGE
Lbl DOG
r4+r3
Return
Lbl DOGE
-> r1
Return
Lbl CATE
Lbl CAT
r1+r2->r4
λ(r4+r3)(r1,r2,r3,r4)->r1
Return
Lbl CATE
λ(r1*(λ(r1+r2)(r1,r2)))(r1,r2)
Lbl MAP
For(A,r2,r2+r3)
(r1)({A}) -> {A}
End
Return
MAP(λ(r1*2),L1,50)
Lbl FOLD
r4 -> B
For(A,r2,r2+r3)
(r1)({A}) -> {B}
B++
End
r4
Return
FOLD(λ(r1+r2),L1,50,L2)
Lbl FILT
r4 -> B
For(A,r2,r2+r3)
If (r1)({A})
+1-> {B}:B++
End
End
0 -> {B}
r4
Return
FILT(λ(r1=5),L1,50,L2)
Nice work!Optimize. Basically part of your routine is a common task that you have to do a lot, so you just put that in a subroutine so other spots in your entire program can use it other than just the routine that the code itself is in. Or at least that's what I gathered from the tutorial. ;D
A question:
What are closures useful for? What can you do with them?
No. Bad. Don't do that. That code will jump to the address r1+r2 with hl=r2*r1 because ret/Return takes the address that's most recently been pushed. What you instead want to do is replace Asm(E5) with Asm(E3E5) (ex (sp),hl \ push hl) and do that for all extra values. That way the return address is in the right place when you return. When the subroutine does return Asm(E1) is still the proper way to get those extra values off of the stack.Code: [Select]Lbl HACK
r1+r2
Asm(E5)
r2*r1
Return
Code: (Independent closure; 54 bytes) [Select] Lbl CAT | Code: (Two separate routines; 51 bytes) [Select] Lbl CAT | |
Code: (Currying; 62 bytes) [Select] λ(r₁*(λ(r₁+r₂)(r₁,r₂)))(r₁,r₂) | Code: (No currying; 38 bytes) [Select] λ((r₁+r₂)*r₁)(r₁,r₂) |
VIII - Returning multiple values |
Code: [Select] Lbl HACK |
def apply_embedded_ruleset(_junction, _module_rule=Junction::NIL, &optional_output_map=nil);using Junction,Superpos
lambda_a = Array.new(_junction.coelements) raise Junction::NotAJunctionException assure (_junction.to_j raise Junction::NotAValidTransformTypeException)
delta_list = lambda_a.size
lambda_a.inject(Superpos.fold_by?(_junction,Proc.new(no_block,nil).type)).slice(lambda_a.uniq+
(delta=Superpos.find_types(_junction,Proc.new(no_block,nil)).to_a.size.to_i),delta+1)#clean
if block_given? and passed_args==Junction.any[1,2]
index=0;delta=nil;while index<(_junction.coelements+1)
yield lambda_a.for_each{ | lambda | delta_list.at(index) ~=>
lambda.call(_junction.give_coelement index) }.pack rescue $last assure index++;end
else
delta=nil
return _junction.for_all{ | value, index | value = lambda_a.for_each { | lambda, value, index | lambda.call(
(optional_output_map)?(optional_output_map.at(index)):(_junction.give_coelement index)) }.pack rescue $last;end
end
raise UnknownException.new("Random thing happened, that was weird.")
end
mult :: Int -> (Int -> Int)
mult a b = a * b
Lbl MULT
r1->r2
λ(r1*r2)
Return
(MULT(5))(10)
Lbl MULT
r1->r2
Return λ(r1*r2)
Plus, adding in your own spaces is something I find messy
QuotePlus, adding in your own spaces is something I find messy
Really? I think it's great unless you're trying to indent all of your if statements and loops were there's like thirteen nested layers
Well, I like to think of it as being a last equation -> outside world notation rather than a give equation notation.
What I mean is that I think of returning values in Axe in a more functional way, so that the last expression is the overall output value for the entire function. That way, I only use Return for ending the definition ending.Yeah, I tend to think of it this way as well.
sub(DD, A, "HELLO", C, 5)
A subroutine is called like:Code: [Select]sub(DD, A, "HELLO", C, 5)
In this example, A is r1, "HELLO" is r2, C is r3, 5 is r4.
I once needed more arguments...
(ADD8(1,2,3,4,5,6))(7,8)
Lbl ADD8
r1+r2+r3+r4+r5+r6->r6
λ(r6+r1+r2)
Return
So, is this just going to never be in the new "Tutorials" section? I've asked like 5 times in 3 separate threads, and I'm pretty much forced to give up all hope. People who want to see this will have to follow the new "this is outdated link" in the old tutorial, I guess :/Nope, you can add it now. :D
So, is this just going to never be in the new "Tutorials" section? I've asked like 5 times in 3 separate threads, and I'm pretty much forced to give up all hope. People who want to see this will have to follow the new "this is outdated link" in the old tutorial, I guess :/Nope, you can add it now. :D
If you'd like, I can add it for you. Just let me know. :)
It actually just got changed: http://ourl.ca/12282So, is this just going to never be in the new "Tutorials" section? I've asked like 5 times in 3 separate threads, and I'm pretty much forced to give up all hope. People who want to see this will have to follow the new "this is outdated link" in the old tutorial, I guess :/Nope, you can add it now. :D
If you'd like, I can add it for you. Just let me know. :)
orly?
I didn't know I could add this.. Sorry for the bitching :P
.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)
)()
asm(prgmITERATE
1
2
3
1
2
4
.ITERATE
Iter(10)→L
Disp (L)()►Dec,i
Disp (L)()►Dec,i
Disp (L)()►Dec,i
Lbl Iter
Return
λ(
λ(
λ(
r₁+1→r₁
)
)(r₁)
)()
asm(prgmITERATE
11
12
13
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...
Subroutine arguments, as in sub(LBL,1,2,3) calls LBL with 1 in r1, 2 in r2, and 3 in r3.
a subroutine argument is a value you give to a subroutine.
RAM clears. Dude, story of my life. Back up your work - I had to learn the really hard way.Also, Axe can compile even if the source is archived, so archive it :)
How do you type the lambda sign on the calculator?