Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


FP (by Backus) -- one liners filter_list
Author
Message
FP (by Backus) -- one liners #1
Hello guys,

I had to do a lecture about John Backus (inventor of FORTRAN, the first high level language) and his paper Can programming be liberated from the von Neumann style? (if you want to read it, be prepared to see a lot of mathematical proofs)

Backus didn't only invent FORTRAN, but also FP - a functional language. He uses it as a proposal how to get away from the von Neumann languages and to prove his points in the paper. Trying it was very interesting. So for everyone who likes functional programming, you might like this one too.

I found this compiler for fp, which I will use for the following programs: http://www.call-with...nuation.org/fp/

And here are the one liners I created (I will explain them right after):

Code:
product = /mul
eq0 = eq . [id, ~0]
ip = /add . @product . trans
mm = @@ip . @dl . dr . [s1, trans.s2]
factorial = eq0 -> ~1; product . [id, factorial . sub1]

The last three are the interesting parts. ip stands for inner product and will take a sequence with vectors, for example like this:

Code:
product = /mul
ip = /add . @product . trans

input = ~<<1, 2, 3>,<6, 5, 4>>

main = (do
    emit.(return ~"nInputn")
    show.(return input)
    emit.(return ~"nTransposen")
    show.(return trans.input)
    emit.(return ~"nApplyToAll productn")
    show.(return @product.trans.input)
    emit.(return ~"nInsert addn")
    show.(return ip.input)
)

I added output for every step, so you can understand how this one liner works. You solve it from left to right. Hint: This would be the same in Haskell:

Code:
( foldr1 (+) . map product . transpose ) [[1,2,3],[4,5,6]]

And this is how the computation is done:

Code:
/add .  @product  . trans ~;< 6; 5; 4 >>
/add .  @product ~;< 2; 5 >;< 3; 4 >>
/add ~< 6; 10; 12 >
28

The factorial:

Code:
product = /mul
eq0 = eq . [id, ~0]

factorial = eq0 -> ~1; product . [id, factorial . sub1]

input = ~< <<2,2>,<2,2>>, <<1,1>,<4,4>>  >

main = (do
    emit.(return ~"nInput:n")
    show.(return input)
    emit.(return ~"nFactorials:n")
    show.(return @factorial.input)
)

The matrixmultiplication (mm):

Code:
product = /mul
ip = /add . @product . trans

mm = @@ip . @dl . dr . [s1, trans.s2]

input = ~<0,1,2,3,4,5,10>

main = (do
    emit.(return ~"nInputn")
    show.(return input)
    emit.(return ~"n[s1, trans.s2]n")
    show.(return [s1, trans.s2].input)
    emit.(return ~"nDistributeFromRightn")
    show.(return dr.[s1, trans.s2].input)
    emit.(return ~"nApplyToAll DistributeFromLeftn")
    show.(return @dl.dr.[s1, trans.s2].input)
    emit.(return ~"nApplyToAll ApplyToAll InnerProductn")
    show.(return mm.input)
)
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply







Users browsing this thread: 1 Guest(s)