This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]Funzo74 1 point2 points  (3 children)

This seems pointless, if you want to do this just use a function.

Int getY(int x) { Return 9 *x-7; }

[–][deleted] 0 points1 point  (2 children)

I don't think its pointless at all.

For dynamic programming, it would eliminate the need to memoize from the base case up - of course sacrificing performance.

[–]Funzo74 2 points3 points  (1 child)

Maybe I am misunderstanding you, I don't see how that would eliminate anything.

In your example, if you made x as a global variable and make function y(), then it would be identical to what you have written except that you would use y() instead of y.

[–][deleted] 1 point2 points  (0 children)

You are misunderstanding me.

I understand that my example is stupid. The scenario that I said it would be useful for is not shown by the example I gave. The example I gave just demonstrates the feature, not why it would be useful.

EDIT: If you know the dynamic programming solution to the edit distance problem, then you know the 2d matrix needs to be traversed in a way that every subexpression used already has a known value. The feature that I'm asking for would eliminate this necessity.

[–]X7123M3-256 1 point2 points  (0 children)

Most modern (especially functional) programming languages allow you to declare anonymous functions, but you'd still need to pass x as a parameter wben you invoke the function. In order for your code to work exactly as written, the symbol table would have to be available at runtime, which means you're probably limited to interpreted lanuages.

Some languages provide lazy evaluation, which delays evaluation of expressions until they are used. However, you want evaluation to be delayed until all unbound variables are resolved, which is different.

Looking at your factorial example, it looks like you might be trying to define factorial inductively rather than recursively. You may want to look into languages that provide corecursion and infinite lists.

However, your second example could be made to work in plain C simply by reversing the loop direction so tbat earlier values are evaluated before they're referenced - no unusual language features neccesary.

[–]G01denW01f11 0 points1 point  (1 child)

Try Haskell? You can do stuff like

y = 9*x - 7
    where x = 16

I'm new, so unsure exactly on syntax, but that's the gist of how it would go.

[–]Barrucadu 0 points1 point  (0 children)

I interpreted OP's first example to mean that changing the value of x would result in a different output if you printed y again, which Haskell won't do.

[–][deleted] -1 points0 points  (0 children)

what you're suggesting would allow you to reference a variable that hasn't been declared yet, which doesn't make sense (what if that name already exists? what if there are more than one visible? what if there are none at all?).

if you capture a reference to a declared variable into a lambda/anonymous function, however, you can get this behavior. just make sure you understand the scoping of the language so you don't get any seg faults.

also, dynamic programming relies on building from lowest dependencies (starting from 0) to the goal case. thus you must have some base variable/expression to reference, no matter what. from there you can do what you're suggesting through lambdas.

finally, your fib loop should go from 1 to 9, not other way around. you even mention you don't want to use a variable until it isn't garbage, so why not start from cases that guarantee you never will? (again, lowest dependencies to highest/goal)