Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fiddling with point-free code?

I have been learning the Factor and J languages to experiment with point-free programming. The basic mechanics of the languages seem clear, but getting a feeling for how to approach algorithm design is a challenge.

A particular source of confusion for me is how one should structure code so that it is easy to experiment with different parameters. By this, I mean the sort of thing Mathematica and Matlab are so good at; you set up an algorithm then manipulate the variables and watch what happens.

How do you do this without explicit variables? Maybe I'm thinking about this all wrong. How should I approach this in point-free programming?

like image 298
user287424 Avatar asked Aug 28 '10 19:08

user287424


Video Answer


1 Answers

Here are three important advices that I found really helpful while dealing with the concatenative paradigm (applied to the Factor programming language in my case):

  • Factor your code mercilessly. Write extremely small functions: if there is more than 3-4 stack parameters, maybe you could break it into smaller parts.
  • Invest your time in learning dataflow combinators (bi, tri, cleave, spread,...). They allow to express common dataflow patterns while removing the need of complex stack shuffling.
  • Learn to build quotations from other quotations. Use currying techniques (curry, with, ...) to build simple quotations from stack parameters, and when things get too complex use Fried quotations (the "fry" vocab). They allow to easily build complex nested quotations from patterns, without any stack shuffling.

And as a always, read and "Walk" into existing code. In Factor it is quite easy to explore the runtime and see how things are working.

For your specific source of confusion, if you have a lot of input parameters in your algorithm, the most important thing to do is to study how they will be used. Harvest for dataflow patterns. You must really THINK about the best way to "schedule" operations on the smallest set of related parameters.

It is a quite difficult experience, but it is also really rewarding one when it succeed. We feel like a human compiler after that..

Good luck!

like image 197
Gabriel Cuvillier Avatar answered Nov 11 '22 23:11

Gabriel Cuvillier