Truly declarative language?

后端 未结 19 2193
梦毁少年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:21

    Groovy and the magic of closures.

    def (x, y) = [ 10, 20 ]
    
    def z = { x + y }
    
    assert 30 == z()
    
    x = 50
    
    assert 70 == z()
    
    def f = { n -> n + 1 }  // define another closure
    
    def g = { x + f(x) }    // ref that closure in another
    
    assert 101 == g()       // x=50, x + (x + 1)
    
    f = { n -> n + 5 }     // redefine f()
    
    assert 105 == g()      // x=50, x + (x + 5)
    

    It's possible to add automagic memoization to functions too but it's a lot more complex than just one or two lines. http://blog.dinkla.net/?p=10

提交回复
热议问题