【问题标题】:Difference between read-string and load-string in ClojureClojure 中读取字符串和加载字符串的区别
【发布时间】:2010-12-29 13:54:14
【问题描述】:

我有一些代码可以在用加载字符串替换读取字符串后工作。代码有效,但我想知道为什么。这两个clojure函数有什么区别?

【问题讨论】:

    标签: clojure


    【解决方案1】:

    使用 load-string 顺序读取和评估包含在 字符串

    使用read-string从字符串s中读取一个对象

    (均引用自Clojure API

    Load-string 会将您的字符串作为 Clojure 表达式求值,而 read-string 将字符串作为找到的数据结构返回,在这种情况下可能是一个表达式。

    Protip:(load-string "(+ 1 2)")(eval (read-string "(+ 1 2)")) 会给出相同的结果,即 3

    【讨论】:

      【解决方案2】:

      两个显着的区别:

      read-string 读取但不评估,而load-string 读取并且评估。

      (read-string "(inc 1)") => (inc 1)
      (load-string "(inc 1)") => 2
      

      read-string 只读取字符串中的第一个表单,而load-string 加载字符串中的所有表单。

      (read-string "1 2") => 1
      (load-string "1 2") => 2
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-29
        • 1970-01-01
        • 2012-05-22
        • 2010-09-17
        • 1970-01-01
        相关资源
        最近更新 更多