Truly declarative language?

后端 未结 19 2176
梦毁少年i
梦毁少年i 2021-02-04 01:26

Does anyone know of a truly declarative language? The behavior I\'m looking for is kind of what Excel does, where I can define variables and formulas, and have the formula\'s re

相关标签:
19条回答
  • 2021-02-04 02:08

    Have you seen Resolver One? It's like Excel with a real programming language behind it.

    0 讨论(0)
  • 2021-02-04 02:09

    Here is Daniel's example in Python, since I noticed you said you tried it in Python.

    x = 10
    y = 10
    
    z = lambda: x + y
    
    # Output: 20
    print z()
    
    x = 20
    
    # Output: 30
    print z()
    
    0 讨论(0)
  • 2021-02-04 02:09

    It's not what you're looking for, but Hardware Description Languages are, by definition, "declarative".

    0 讨论(0)
  • 2021-02-04 02:09

    This F# code should do the trick. You can use lazy evaluation (System.Lazy object) to ensure your expression will be evaluated when actually needed, not sooner.

    let mutable x = 10;
    let y = 20;
    
    let z = lazy (x + y);
    x <- 30;
    
    printf "%d" z.Value
    
    0 讨论(0)
  • 2021-02-04 02:15

    It looks like you just want to make Z store a function instead of a value. In C#:

    var X = 10;    // define and assign two variables
    var Y = 20;
    
    Func<int> Z = () => X + Y;  // declare a formula that uses these two variables
    
    Console.WriteLine(Z());
    
    X = 50;     // change one of the input variables
    
    Console.WriteLine(Z());
    

    So the equivalent of your ?-prefix syntax is a ()-suffix, but otherwise it's identical. A lambda is a "formula" in your terminology.

    Behind the scenes, the C# compiler builds almost exactly what you presented in your C# conceptual example: it makes X into a field in a compiler-generated class, and allocates an instance of that class when the code block is entered. So congratulations, you have re-discovered lambdas! :)

    0 讨论(0)
  • 2021-02-04 02:15

    There is a Lisp library with this sort of behaviour:

    http://common-lisp.net/project/cells/

    0 讨论(0)
提交回复
热议问题