Hacker News new | ask | show | jobs
by _random_ 4218 days ago
Still waaay too imperative, how about this:

func [1,2,3,4,5] => [2,4,6,8,10] //Calculation inferred by compiler.

console.log (func [6,7,8,9,10]) //=> [12,14,16,18,20]

3 comments

One practical way to implement this would be a “...” operator which would look at the syntax of the surrounding expression and attempt to infer an inductive definition from that, based on some assumptions about e.g. the structure of lists or the values of integers.

    double xs => [xs[0] * 2, ..., xs[xs.length - 1] * 2]

    map f xs => [f(xs[0]), ..., f(xs[xs.length - 1])]

    sum xs => xs[0] + ... + xs[xs.length - 1]

    foldl f z xs => f(f(..., f(z, xs[0])), xs[xs.length - 1])

    foldr f z xs => f(xs[0], f(..., f(xs[xs.length - 1], z)))
What algorithms are there for inferring the calculation?

I'm playing around with a tool for building touch gestures visually, and I have some problems that look a bit like that (want to infer a function from some examples) but I don't yet know how.

This calculation is underdetermined, and there isn't any algorithm that would specify "it", since there are many functions that would satisfy the requirements. In general this is a hard problem of induction. In a more limited context you might think about adding regularizations that will make the function better determined. Choosing these and implementing them may not be trivial.

If you are talking paths generated from touch gestures there is some research on that topic.

People working on program synthesis consider the Occam razor "the shortest/simplest code" to be a good heuristic of the intended function (given a good test case.) Their main problem is the exponential space search.
Sounds like a regression problem? The actual algorithm will depend on how well you need to approximate the function, the nature and amount of example data.

http://en.wikipedia.org/wiki/Machine_learning#Approaches

Not practical, there are infinitely many functions that could produce such result

here is such psuedo code foo(x) { if (x <= 5) { x * 2 } else { x } } foo1(x) { if (x <= 6) { x * 2 } else { x+1 } } foo1(x) { if (x <= 7) { x * 2 } else { x+2 } }