读取文件:语法错误(标识符后有多个表达式)

发布于 2024-12-23 07:52:06 字数 321 浏览 2 评论 0原文

(define bootstrap-c-code
    (define (from-file file-name)
       (let* ((ip (open-input-file file-name))
            (res (read-text-file-from-input-port ip)))
         (close-input-port ip)
         res))
    (from-file "llvm.c"))

错误:定义:语法错误(标识符后有多个表达式)

但我看不出它有什么问题。有人可以解释/修复它吗?

(define bootstrap-c-code
    (define (from-file file-name)
       (let* ((ip (open-input-file file-name))
            (res (read-text-file-from-input-port ip)))
         (close-input-port ip)
         res))
    (from-file "llvm.c"))

Error : define: bad syntax (multiple expressions after identifier)

But I can't see anything wrong with it. Can someone explain / fix it please.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的快感 2024-12-30 07:52:07

目前尚不清楚您使用上述代码的意图。如果您尝试加载文本文件并将加载的值保留在名为 bootstrap-c-code变量中,请尝试以下操作:

(define bootstrap-c-code
  (let ((from-file
         (lambda (file-name)
           (let* ((ip (open-input-file file-name))
                  (res (read-text-file-from-input-port ip)))
             (close-input-port ip)
             res))))
    (from-file "llvm.c")))

当然,from -file定义仅在let内部可见,如果需要在外部使用它,请在整个表达式之外define它。如果您只需要 let 中的 from-file 功能,则可以通过更简单的方式获得相同的结果:

(define bootstrap-c-code
  (let* ((ip  (open-input-file "llvm.c"))
         (res (read-text-file-from-input-port ip)))
    (close-input-port ip)
    res))

另一方面,如果您的意图是创建一个名为 bootstrap-c-code过程,那么正确的语法是:

(define (bootstrap-c-code)
  (define (from-file file-name)
    (let* ((ip (open-input-file file-name))
           (res (read-text-file-from-input-port ip)))
      (close-input-port ip)
      res))
  (from-file "llvm.c"))

It's not clear what you intended with the above code. If you were trying to load a text file and leave the loaded value in a variable called bootstrap-c-code, then try this:

(define bootstrap-c-code
  (let ((from-file
         (lambda (file-name)
           (let* ((ip (open-input-file file-name))
                  (res (read-text-file-from-input-port ip)))
             (close-input-port ip)
             res))))
    (from-file "llvm.c")))

Of course, the from-file definition will only be visible inside the let, if you need to use it outside, define it outside of the whole expression. If you only need the functionality of from-file inside the let, you can obtain the same result in a much simpler way:

(define bootstrap-c-code
  (let* ((ip  (open-input-file "llvm.c"))
         (res (read-text-file-from-input-port ip)))
    (close-input-port ip)
    res))

On the other hand, if what you intended was to create a procedure called bootstrap-c-code, then the correct syntax would be:

(define (bootstrap-c-code)
  (define (from-file file-name)
    (let* ((ip (open-input-file file-name))
           (res (read-text-file-from-input-port ip)))
      (close-input-port ip)
      res))
  (from-file "llvm.c"))
萧瑟寒风 2024-12-30 07:52:07

根据R5RS,内部定义只能出现在开头一堆形式的组成,如 let、let*、lambda 等。就您的代码而言,情况并非如此,因为您在非过程定义中有一个内部定义。您可以通过将“bootstrap-c-code”绑定到过程来修复它。

According to R5RS, internal definitions can occur only at the beginning of the of a bunch of forms like let, let*, lambda etc. In the case of your code, that is not the case since you have an internal definition inside a non-procedural define. You could fix it by making `bootstrap-c-code' bind to a procedure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文