Author Topic: [Tutorial]Achieving Decent Functional Programming in Axe DEPRECATED  (Read 2572 times)

0 Members and 1 Guest are viewing this topic.

Ashbad

  • Guest
NOTE BY AUTHOR: this tutorial is now outdated.  You can still use everything here since it's all valid Axe 1.0.0 code, but since 1.0.0 support for better functional programminng has been added.  I rebooted this tutorial and covered everything in new notation here: http://ourl.ca/12120


Guide to using Functional Paradigmal patterns in Axe

Hello, today I would like to share some things I noticed you can do with Axe to use it in a more high-level functional way.  This tutorial assumes you don't know how higher level functional concepts work and walks you through all of them step by step.  Axe has limited support for functions, many think, but that's pretty far from the truth.  You just can't know all of them from reading a documentation, some of them are either more of reactions that the compiler would do based on its function, or with the help of a little inline assembly.  Here are a few things that when used with your program, can make it flow much better and in some cases achieve things that couldn't be done normally with built in features.


Returning Values

This is one of the more simpler concepts that you can draw from functional Axe programming.  Basically, when you return a value from a function, you're simply just letting the process of that function represent a value.  That value is determined by the function, which does arithmetic and possibly even further computational things to obtain that value.  Let's start with this non-functional code for example:

Code: [Select]
sub(ADD,5,5)
If A = 10
   Disp "Lol"
End

Lbl ADD
  r2 + r1 -> A
Return

This can be simply improved like so:

Code: [Select]
If sub(ADD,5,5) = 10
  Disp "Lol"
End

Lbl ADD
  r2 + r1
Return

This is a decent optimization in size, and speed.  Plus, you have no need to store the answer value from ADD into a variable, we can just use the call to ADD itself as a value.  Why does this work like this?  Because HL, the assembly register, is like the Ans with BASIC but with Axe.  The parser puts all expressions that were just parsed into HL, so that's why "r2 + r1" returned 10 into HL, which was then used by the calling If statement.


Simple Variable-Attachment Lambdas

What is a Lambda statement?  Essentially, it's the most value-like function attainable.  It is literally a stand-in for a real value that it returns.  Lambdas are very useful in functional programming, but are harder to use in Axe.  They come in two forms:  Normals, and Procs.  I'll start with procs, since Normals are usually attached to a variable (easy to do in HLLs, not easy in Axe, procs are easier to use in straight-up axe).  Since they can literally stand into a part of a program in any place, here is an example of a Proc'd expression:

Code: [Select]
Lbl PRC:r1+3:Return
If sub(PRC,rand^4 -> A) = 6
  Disp "'tis 6"
Else
  Disp "No, it's actually",sub(PRC,A) >Dec
End

As you can see, a Proc in Axe is literally just a subroutine used in place of a value exclusively.  While that may not be super-useful with small things, it saves a lot of space with constant expression checking, though the speed will generally take a toll so minor that it really woudln't matter.


Normals

Normals can be achieved by attaching a Proc to a variable, so that whenever the variable is accessed its value depends on input parameters.  Here is one way of doing so, where you can actually get the value of a variable but in Normallic form using variable indirection and a simple proc:
º
Code: [Select]
If sub(PCV,ºA) = 5
  Disp "THIS IS 5."
End

Lbl PCV .The proc for attaching
  {r1}r + 5 -> {r1}r
Return

This Lambda follows Normallic form, but it doesn't completely attach itself to a variable.  To do that so it's variable specific, you can do this:

Code: [Select]
If sub(CA) = 5
  Disp "THIS IS 5."
End

Lbl CA .Call A
  sub(PCV,ºA)
Return
Lbl PCV
  {r1}r + 5 -> {r1}r
Return

An alternative to that, where you only use one routine to do both, but it only applies to one:

Code: [Select]
If sub(CA) = 5
  Disp "THIS IS 5."
End

Lbl CA
  {ºA}r + 5 -> {ºA}r
Return


Mapping and Folding

Mapping and folding in Functional programming basically are functions that apply to all members of an Array, List, etc.  Basically, they loop and fix all numbers one by one until the end of array or the specified length has been reached.  An example:

Code: [Select]
Data(5,6,2,1,0) -> Str1
sub(MAP,Str1)

Lbl MAP
  For(A,0,length(r1))
    {r1+A} + 5 -> {r1+A}
  End
Return

This above method applies this map to a zero terminated array.  To d the same with length termination is a simple difference:

Code: [Select]
Data(5,6,2,1) -> Str1
sub(MAP,Str1,4)

Lbl MAP
  For(A,0,r2)
    {r1+A} + 5 -> {r1+A}
  End
Return


Folding is almost the same, but it fills in more of the place of a value-function than a performance one.  Basically, a Fold is a Map that returns values in the form of any type of object, either in a fixed array leaving the original the same, or even outputting a list of return values in array form afterwards so a programmer can see if certain conditions were met.  Examples:

Fixing an array without being destructive, by outputting results to a result array:

Code: [Select]
Data(1,2,3,0) -> Str1
L1 -> P . Pointer to result array
sub(FLD,Str1,P)

Lbl FLD
  For(A,0,length(r1))
    {r1+A} + 5 -> {r2+A}
  End
Return


Fixing and checking to see if fixed elements are 10, and output results to an output array: 

Code: [Select]
Data(1,2,3,0) -> Str1
L1 -> P . Pointer to output array
sub(FLD,Str1,P)

Lbl FLD
  For(A,0,length(r1))
    {r1+A} + 5 -> {r1+A}
    If {r1+A} = 10
      1 -> {r2 + A}
    Else
      0 -> {r2 + A}
    End
  End
Return


Filters

Filters are the next type of higher level functional forms.  Filters are simply "checkers" that see if an array has certain elements that fulfill a certain expressional requirement, and returns either the element contents or indices to the passing elements.  In many cases with axe, due to limited functional properties already, returning indices is better since you can have more detail on the elements and can do more operations on them, while still being able easily reproduce the exact value of the passing elements.  A thing to remember with filter outputs though -- usually, the output for the filter will be an array less than or equal to the size of the inout array -- though in rare cases, with certain passing requirements or double looping checks, they can be larger.  But, with the output of filters being so variant in size you need to either take precautions to make sure that if zero is a passing value, you must return the length of the output array.  You must also be sure you have error checking for no-match conditions.  Anyways, examples:

This code filters the array to see if the elements are equal to 1, and returns indices to the passing elements:

Code: [Select]
Data(1,2,2,1,1,3,0) -> Str1
L1 -> P
sub(FLT,Str1,P)

Lbl FLT
  0 -> B
  For(A,0,length(r1))
    If A*({r1+A}=1}
      -> {r2+B}
      B++
    End
  End
Return

To return a size of the output array, you can just return B value-style since we're already sure that the output pointer is defined already, you can change it to this:

Code: [Select]
Data(1,2,2,1,1,3,0) -> Str1
L1 -> P
Disp "Size of array: ",sub(FLT,Str1,A) >Dec

Lbl FLT
  0 -> B
  For(A,0,length(r1))
    If A*({r1+A}=1}
      -> {r2+B}
      B++
    End
  End
  B
Return


Returning multiple arguments

Let's say you want to make a Filter function that assumes that you want the output array to be placed somewhere in memory assigned by the equivalent of a malloc() command.  You can return multiple arguments by simply packing all of the returned values into an array of unorganized members.  You then return the pointer to that array, and the calling expression can unpack it as needed.  Example:

Code: [Select]
Data(1,2,1,2,3,1) -> Str1
sub(MRF,Str1) -> P
Disp {P+2} >Dec," Passing elements found, which are at indices: ",i
For(A,0,{P}r)
  Disp {{P}r+A} >Dec,", "
End

Lbl MRF
  ... CALL MALLOC COMMAND
      AND DELEGATE SOME 
      MEMORY AND GIVE A 
      POINTER IN P ...
  0 -> B
  For(A,0,length(r1))
    If A*({r1+A}=1}
      -> {P+B}
      B++
    End
  End
  ... CALL MALLOC COMMAND 
     AND DELEGATE SPACE THAT
     IS B BYTES LONG, POINTED TO
     BY Q ...
  P -> {Q}r . Load P into first two bytes
  B -> {Q+2} . Load length int 3rd byte
  Q . Return pointer to return array
Return


That covers a lot of functional programming in Axe for now, but there can always be more added.  If you have anything to contribute to this set of functional ideals, please post them, and help the effort to bring the Functional Paradigm to Axe a reality by informing others ;) Thanks! :)



~~Thanks for reading, more coming! Feel free to ask Q&C's, or ask for more content I can add ;) ~~
« Last Edit: July 17, 2011, 09:38:44 pm by Ashbad »

Offline aeTIos

  • Nonbinary computing specialist
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3913
  • Rating: +184/-32
    • View Profile
    • wank.party
Re: Functional Axe
« Reply #1 on: June 16, 2011, 03:28:27 pm »
great. just great. Well done.
I'm not a nerd but I pretend:

Offline yrinfish

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 138
  • Rating: +8/-0
  • Zark, I'm not dead
    • View Profile
    • yrinthings
Re: Functional Axe
« Reply #2 on: June 16, 2011, 05:08:24 pm »
Wow. The only thing is that I don't understand lambdas anymore...
WIKIIIII

More please!!
I need help with Random (x86 Happy), pm me if you want to help :D
LOOK IN MY SPOILER, MY SIG IS UPDATED (almost) EVERY DAY
Spoiler For MiniTale:
No mini competition please, teamwork is better :D
I suggest we do not use "MiniCraft" as the name:
Notch has asked us to not do that.
It will get changed to "MiniTale"
@Hayleia, you don't _have to_ organize :P
THIS POST IS IN MY SIG TOO

Details
Language: Axe
Platform: TI 83+

Things we'll need to do/make
Draw the sprites saintrunner is working on this
A save file format.
A level generator.
A Tilemapper.
Some sort of AI.
Crafting system.

People on it (AFAIK)
annoyingcalc
saintrunner
Hayleia (your avatar is really cool btw)
epic7
Blue Raven
yrinfish

Useful info
For those who are going to work on this
General Program Structure
Spoiler For Spoiler:

:.Sprite Data
:.Other Data
:
:.Main menu
:.Option Start Game->init
:.Option How To Play->help
:.Option About->crdt
:
:.BEGIN init
:.Save file is present?
:.yes->strt
:.no->lgen
:.END init
:
:.BEGIN lgen
:.Make appvar appvMCSAVE
:.create 7 levels
:.edit for sky, mines, deep mines, nether
:.END lgen
:
:.BEGIN strt

~~~~ working on this ~~~~

Tiles
Spoiler For Spoiler:
Wheat
Water
Tree
Stone
Stairs
Sapling
Sand
Rock
Ore
Lava
InfiniteFall
Hole
HardRock
Grass
Flower
Farm
Dirt
Cloud
CloudCactus
Cactus

Biomes
Spoiler For Spoiler:
--Sky
Cloud
--Surface
Forest
Meadow
Plains
Cliffs
Mountain
Desert
Ocean
--Underground
Mines
Deep Mines
Nether

Items
Spoiler For Spoiler:
--Tools
Pick
Axe
Hoe
Shovel
--Weapon
Sword
--Utilities
Pow Glove
Workbench
Furnace
Lantern
Anvil
Oven
Chest
--Resources
Apple
Bread
Cloth
Slime
Wood
Seeds
Wheat
Acorn
Cactus
Flower
Cloud
Dirt
Stone
Sand
Coal
Iron Ore
Gold Ore
Gem
Glass
Iron
Gold
Spoiler For More...:
Spoiler For About me:

No, I didn't lie!!


quotes:HERE

And even more:


[07:53:14] <+Frey> Y'know, when people descend from my heavens , they're never Chuck Norris
[07:53:55] <+Frey> I usually get another catgirl or an angel pops down for a visit
[07:54:06] <+Frey> Some angels are really nice
[07:54:33] <+Frey> Some are into the whole "scare the locals" bit
[07:54:54] <+Frey> And some just absent minded
[07:55:38] * +david_ has nothing to do
[07:55:40] <+Frey> It's the absentminded ones you gotta look out for, because the'yre the ones that forget they're 40ft tall
[07:56:04] <+Frey> They're also the ones that forget about social taboos, like nudity
[07:56:28] <+Frey> Have you ever tryed to explain to an angel...anything?
[07:56:45] <+Frey> They don't listenunless they want to
[07:57:16] <+Frey> And don't learn unless they think they don't know
[07:57:51] <+Frey> Except for the nice ones, they're nice
[07:59:27] <+OmnomIRC> (O)<yrinfish> lol
[07:59:32] <+Frey> And they're all so pretty!
[07:59:38] <+Frey> It's ridiculous
[07:59:44] <+OmnomIRC> (O)<yrinfish> I hate PUSHES, THEY HAUNT ME
[08:00:06] <+Frey> Want an angel to come over and exorcise them?
[08:00:36] <+OmnomIRC> (O)<yrinfish> or, more specific: the fact that sometimes, happy outputs: pushes != pops
[08:00:47] <+OmnomIRC> (O)<yrinfish> I know why
[08:01:01] <+OmnomIRC> (O)<yrinfish> and I can solve it by adding a lookahead
[08:01:10] <+OmnomIRC> (O)<yrinfish> But I don't want to
[08:01:24] <+OmnomIRC> (O)<yrinfish> but continue your story
[08:01:35] <+OmnomIRC> (O)<yrinfish> lol
[08:03:13] <+Frey> Ok, so one day, I was walking to the school bus, and, long story short, got my life saved by an angel
[08:03:27] <+Frey> This one was a scary one
[08:03:42] <+Frey> Same height/age as me
[08:04:14] <+Frey> OMG She was the scariest person I had ever met
[08:04:28] <+Frey> She made the entire football team pee their pants
[08:04:43] <+Frey> I was so glad she was on my side
[08:05:04] <+Frey> Right up until she tryed to enter the bathroom with me
[08:05:37] <+OmnomIRC> (O)<yrinfish> lool
[08:05:45] <+Frey> After a muchness of arguing, I finally got through to her
[08:05:56] <+Patrick-D> is this a rap song?
[08:06:06] <+OmnomIRC> (O)<aeTIos> idk
[08:06:09] <+Frey> She tap danced outside my stall the entire time, though
[08:10:04] <+Frey> She was in all my classes too, which was weird, especially as they were all advanced courses and she joined in the middle of the semester
[08:10:28] <+Frey> Even the teachers were scared of her
[08:10:45] <+Frey> Note that she was gorgeous
[08:12:09] <+Frey> So, this angel and I, we managed to stop the apocalypse
[08:12:48] <+Frey> And she kissed me before going back,
[08:13:08] <+Frey> To heaven, but as it turns out that kiss sealed her to Earth
[08:13:47] <+Frey> We still don't know what to do


lol:

[11:30:53] <DThought> <OmnomIRC> (O)<yrinfish> 2 guests are looking at my topic
[11:30:53] <DThought> <OmnomIRC> (O)<yrinfish> I wonder who they are
[11:30:53] <DThought> <OmnomIRC> (O)<yrinfish> where they live
[11:30:53] <DThought> <OmnomIRC> (O)<yrinfish> how old they are
[11:30:55] <DThought> <OmnomIRC> (O)<yrinfish> What kind of jokes they make
[11:30:57] <DThought> <OmnomIRC> (O)<yrinfish> but it surprises me that theyre looking at my topic
[11:31:01] * DThought smacks yrinfish for stalking
[11:31:08] <yrinfish> lool
[11:31:14] <DThought> Whoa, why'd it include all the newlines?
[11:31:23] * calc84maniac  smacks DThought for talking
[11:31:28] * calc84maniac  runs

Crisps:

<OmnomIRC> (O)* Keoni29  Eating crisps
<OmnomIRC> (O)* yrinfish  asks keoni some
<OmnomIRC> (O)<Keoni29> 7$ shipping please
<OmnomIRC> (O)* yrinfish  gives
<OmnomIRC> (O)<Keoni29> It will arrive in 3 days
<OmnomIRC> (O)<yrinfish> Thank you
<OmnomIRC> (O)* Keoni29  Wonders if he wants some dip
<OmnomIRC> (O)<yrinfish> chili please
<OmnomIRC> (O)<Qwerty.55> Adobe, last time I updated one of your products, the changes broke half of the documents using it.
<OmnomIRC> (O)<Keoni29> :(
<OmnomIRC> (O)<Qwerty.55> Why the hell would I update anything else from you?
<OmnomIRC> (O)* yrinfish  kicks adobe
<OmnomIRC> (O)* yrinfish  runs
<OmnomIRC> (O)* Qwerty.55  runs with him
<OmnomIRC> (O)<Keoni29> I have guacamole :S
<OmnomIRC> (O)<Keoni29> I have guacamole :S
<OmnomIRC> (O)* yrinfish  likes that too
<OmnomIRC> (O)* Keoni29  yay
<OmnomIRC> (O)* yrinfish
<OmnomIRC> (O)<Keoni29> http://www.tinyurl.com/guacamole.jar
<OmnomIRC> (O)<Keoni29> :3
<OmnomIRC> (O)<yrinfish> 404
<OmnomIRC> (O)<yrinfish> no foooooooooooooooooooooooouwaaaaaaaaaaaaaaaaahnd
<OmnomIRC> (O)<Keoni29> <3 no shit
<OmnomIRC> (O)<Keoni29> You cant download jars of guacamole
<OmnomIRC> (O)<Keoni29> I have an idea!
<OmnomIRC> (O)<yrinfish> loool
<yrinfish> that came too late ;)
<OmnomIRC> (O)<Qwerty.55> Actually, I just realized why Adobe's update breaks everything.
<yrinfish> tell me
<yrinfish> again too late
<OmnomIRC> (O)<Keoni29> Buy yourself some :3
<yrinfish> that was to keoni
<yrinfish> lOLoL
<yrinfish> :D
<OmnomIRC> (O)<Qwerty.55> It's a security update and if you can't run anything, you obviously can't get infected by anything in those documents.
<OmnomIRC> (O)<Keoni29> Then I pay one dip
<OmnomIRC> (O)<Keoni29> Do you have paypal?
<OmnomIRC> (O)<Keoni29> :S
<OmnomIRC> (O)<yrinfish> nope
<OmnomIRC> (O)<yrinfish> lol
<OmnomIRC> (O)<yrinfish> hm, I could get some crisps
<OmnomIRC> (O)<yrinfish> it is too late to do that here
<OmnomIRC> (O)<yrinfish> main europe
* jkag ([email protected]) has joined #omnimaga
* Netbot45 gives voice to jkag
<OmnomIRC> (O)<yrinfish> MUSIC YAY
<OmnomIRC> (O)<Keoni29> Well the shops are closed here too
<OmnomIRC> (O)<Keoni29> 21:45
<OmnomIRC> (O)<yrinfish> where?
<OmnomIRC> (O)<Keoni29> Gwt+0
<OmnomIRC> (O)<Keoni29> The netherlands
<OmnomIRC> (O)<yrinfish> huh? loool
<OmnomIRC> (O)<yrinfish> hallo keoni, hoe gaat het? continue in english please
<OmnomIRC> (O)<Keoni29> :S
<OmnomIRC> (O)<yrinfish> really stupid dutch sentence^

Languages:
1308401677 <yrinfish> I'm dutch
1308401679 <yrinfish> lol
1308401687 <p2> Oh.
1308401702 <yrinfish> So I need to learn german
1308401705 <yrinfish> and french
1308401712 <yrinfish> and English
1308401717 <yrinfish> and Dutch
1308401724 <yrinfish> and Greek
1308401728 <yrinfish> and Latin
1308401739 <p2> I must learn french and English.
1308401739 <yrinfish> and Spanish
1308401749 <Spyro543> Y=sin(x+yrinfish)*>9000
NOTE: these are all the languages I have chosen to learn!
Spoiler For Info:

Spoiler For Projects:
Happy    [.---------] A C-like programming language for the z80 in js. REWRITING THE WHOLE THING, GOOD IDEAS NEEDED
DionJump [======----] Paused, but there is an update
Corona   [----------] Not enough time... If you need inspiration?
LGETC    [======----] getchar() LUT, useful and not too big. Waits for keyboard input and returns ASCII char

Spoiler For yrinthings tools:
stringLength Get the length in pixels of a small-font string.
safeText Html-ify a piece of text.

Ashbad

  • Guest
Re: Functional Axe
« Reply #3 on: June 16, 2011, 06:51:54 pm »
Glad you like it, and yrinfish I would be glad to point you to some resources for learning how lambdas work if you want ;)

With that being said, I think I'll add some more onto this tomorrow morning or late tonight.


Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Functional Axe
« Reply #4 on: June 16, 2011, 07:01:32 pm »
Quote
Guide to using Axe to its full functional form

I'm not saying your guide isn't useful (+1), but Axe is a procedural/imperative language, not a functional one. Saying that it has a "full functional form" is kind of misleading. It's more accurate to say that you can synthesize other paradigms from imperative languages.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Ashbad

  • Guest
Re: Functional Axe
« Reply #5 on: June 16, 2011, 08:42:48 pm »
That's true :P but since it can be used in primitive functional forms, and since most people don't even take note of them, or even know you can use things like that, I wanted to inform ;) however, I agree, I should change that opening line.

I have now added a ton more to this tutorial, covering more higher level functional aspects:


Mapping and Folding

Mapping and folding in Functional programming basically are functions that apply to all members of an Array, List, etc.  Basically, they loop and fix all numbers one by one until the end of array or the specified length has been reached.  An example:

Code: [Select]
Data(5,6,2,1,0) -> Str1
sub(MAP,Str1)

Lbl MAP
  For(A,0,length(r1))
    {r1+A} + 5 -> {r1+A}
  End
Return

This above method applies this map to a zero terminated array.  To d the same with length termination is a simple difference:

Code: [Select]
Data(5,6,2,1) -> Str1
sub(MAP,Str1,4)

Lbl MAP
  For(A,0,r2)
    {r1+A} + 5 -> {r1+A}
  End
Return


Folding is almost the same, but it fills in more of the place of a value-function than a performance one.  Basically, a Fold is a Map that returns values in the form of any type of object, either in a fixed array leaving the original the same, or even outputting a list of return values in array form afterwards so a programmer can see if certain conditions were met.  Examples:

Fixing an array without being destructive, by outputting results to a result array:

Code: [Select]
Data(1,2,3,0) -> Str1
L1 -> P . Pointer to result array
sub(FLD,Str1,P)

Lbl FLD
  For(A,0,length(r1))
    {r1+A} + 5 -> {r2+A}
  End
Return


Fixing and checking to see if fixed elements are 10, and output results to an output array: 

Code: [Select]
Data(1,2,3,0) -> Str1
L1 -> P . Pointer to output array
sub(FLD,Str1,P)

Lbl FLD
  For(A,0,length(r1))
    {r1+A} + 5 -> {r1+A}
    If {r1+A} = 10
      1 -> {r2 + A}
    Else
      0 -> {r2 + A}
    End
  End
Return


Filters

Filters are the next type of higher level functional forms.  Filters are simply "checkers" that see if an array has certain elements that fulfill a certain expressional requirement, and returns either the element contents or indices to the passing elements.  In many cases with axe, due to limited functional properties already, returning indices is better since you can have more detail on the elements and can do more operations on them, while still being able easily reproduce the exact value of the passing elements.  A thing to remember with filter outputs though -- usually, the output for the filter will be an array less than or equal to the size of the inout array -- though in rare cases, with certain passing requirements or double looping checks, they can be larger.  But, with the output of filters being so variant in size you need to either take precautions to make sure that if zero is a passing value, you must return the length of the output array.  You must also be sure you have error checking for no-match conditions.  Anyways, examples:

This code filters the array to see if the elements are equal to 1, and returns indices to the passing elements:

Code: [Select]
Data(1,2,2,1,1,3,0) -> Str1
L1 -> P
sub(FLT,Str1,P)

Lbl FLT
  0 -> B
  For(A,0,length(r1))
    If A*({r1+A}=1}
      -> {r2+B}
      B++
    End
  End
Return

To return a size of the output array, you can just return B value-style since we're already sure that the output pointer is defined already, you can change it to this:

Code: [Select]
Data(1,2,2,1,1,3,0) -> Str1
L1 -> P
Disp "Size of array: ",sub(FLT,Str1,A) >Dec

Lbl FLT
  0 -> B
  For(A,0,length(r1))
    If A*({r1+A}=1}
      -> {r2+B}
      B++
    End
  End
  B
Return


Returning multiple arguments

Let's say you want to make a Filter function that assumes that you want the output array to be placed somewhere in memory assigned by the equivalent of a malloc() command.  You can return multiple arguments by simply packing all of the returned values into an array of unorganized members.  You then return the pointer to that array, and the calling expression can unpack it as needed.  Example:

Code: [Select]
Data(1,2,1,2,3,1) -> Str1
sub(MRF,Str1) -> P
Disp {P+2} >Dec," Passing elements found, which are at indices: ",i
For(A,0,{P}r)
  Disp {{P}r+A} >Dec,", "
End

Lbl MRF
  ... CALL MALLOC COMMAND
      AND DELEGATE SOME 
      MEMORY AND GIVE A 
      POINTER IN P ...
  0 -> B
  For(A,0,length(r1))
    If A*({r1+A}=1}
      -> {P+B}
      B++
    End
  End
  ... CALL MALLOC COMMAND 
     AND DELEGATE SPACE THAT
     IS B BYTES LONG, POINTED TO
     BY Q ...
  P -> {Q}r . Load P into first two bytes
  B -> {Q+2} . Load length int 3rd byte
  Q . Return pointer to return array
Return


That covers a lot of functional programming in Axe for now, but there can always be more added.  If you have anything to contribute to this set of functional ideals, please post them, and help the effort to bring the Functional Paradigm to Axe a reality by informing others ;) Thanks! :)

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Functional Axe
« Reply #6 on: June 16, 2011, 09:27:14 pm »
Mapping and folding in Functional programming basically are functions that apply to all members of an Array, List, etc.  Basically, they loop and fix all numbers one by one until the end of array or the specified length has been reached.  An example:

implementing mapping, folding and filtering in Axe isn't very efficient though. The power behind those three functions is that they are higher order, which isn't possible in Axe (i.e. you can't pass a function as a parameter). for example, say you want to make 3 separate functions. one that add 5 to every element of a list, one that takes the square root of every element and one that divides each element by 3. in a functional programming language, you would have one function called map, and then three functions, add5(x), sqrt(x), and div3(x). then you'd call map with the parameters of each function and a list. technically you can "map" in Axe, but without the advantages of mapping in functional programming, which IMHO defeats the purpose.

it would be pretty cool to see higher order functions in Axe though.


Ashbad

  • Guest
Re: [Tutorial]Achieving Decent Functional Programming in Axe
« Reply #7 on: June 16, 2011, 09:34:27 pm »
That's true, but you could technically pass those parameters through with certain tricks, which I guess I could show tomorrow when I get back from school.  They aren't very efficient in many cases, but they allow for some doing things in Axe that can't normally be done with non-functional constraints.

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: [Tutorial]Achieving Decent Functional Programming in Axe
« Reply #8 on: June 16, 2011, 09:40:16 pm »
High order functions will be possible in Axe 1.0.0, and will be an interesting paradigm change.  That will probably be THE biggest change from the betas.  However, I'm still not sure if they will be recursively nestable yet, where the parameters can be backed up to the stack, but its still pretty damn useful.
« Last Edit: June 16, 2011, 09:41:22 pm by Quigibo »
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline ralphdspam

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 841
  • Rating: +38/-1
  • My name is actually Matt.
    • View Profile
Re: [Tutorial]Achieving Decent Functional Programming in Axe
« Reply #9 on: June 16, 2011, 11:10:57 pm »
Looking forward to that.  O.O
ld a, 0
ld a, a

Ashbad

  • Guest
Re: [Tutorial]Achieving Decent Functional Programming in Axe
« Reply #10 on: July 14, 2011, 10:20:40 am »
Let it be known this tutorial is deprecated and I'll be posting an updated and expanded version tonight, covering all topics here in more depth with new 1.0.1 notation.

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: [Tutorial]Achieving Decent Functional Programming in Axe DEPRECATED
« Reply #11 on: July 14, 2011, 10:46:09 am »
That's awesome, Ashbad.  After the contest (when I want to learn 1.0.1) I'm certainly going to give this a read-though or two; I would like to learn lambdas and I believe this is the perfect tutorial for that.  Thanks! :D