【问题标题】:Rebol anonymous function behavior is weirdRebol 匿名函数行为很奇怪
【发布时间】:2010-08-07 09:19:52
【问题描述】:

我下面的匿名函数测试只执行一次:

repeat i 5 [
  func[test][
    print test
  ] rejoin ["test" i]
]

我必须将其命名为能够按预期执行 5 次:

repeat i 5 [
  test: func[test][
    print test
  ] test rejoin ["test" i]
]

这很奇怪。是不是真的可以在循环中使用匿名函数?

【问题讨论】:

    标签: functional-programming rebol


    【解决方案1】:

    您的第一个代码示例简单地定义匿名函数五次。它不调用它。添加一个 do,一切都会好起来的:

    repeat i 5 [
      do func[test][
        print test
      ] rejoin ["test" i]
    ]
    
    test1
    test2
    test3
    test4
    test5
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      相关资源
      最近更新 更多