0

鸡计划 4.8.0.5

大家好,

假设我有一个包含顶级定义的文本文件

topLevelDef.txt

(define techDisplays '(
  ( AG1    fillerIgnore    AG1_fillerIgnore    t   t  nil  t  nil )
  ( AG2    drawing         AG2_drawing         t   t  nil  t   t  )
  )
)

我把它作为一个包含

试试this.scm

(use extras format posix posix-extras regex regex-literals utils srfi-13)
(include "./mytech/mytech.tf.techDisplays") ; works when hard-coded
;include inn-file)      ; want to pass it in as an arg

(define write-out-techDisplays
  (lambda()
    (for-each
      (lambda(rule-as-list-of-symbols)
        (begin
          (set! rule-as-list-of-strings ( map symbol->string rule-as-list-of-symbols))
          (print (string-join rule-as-list-of-strings    ))
        )
      )
      techDisplays
    )
  )
)

(define (main args)
  (set! inn-file ( car args))
  (set! out-file (cadr args))
  (with-output-to-file out-file write-out-techDisplays)
0
)

那么我该如何实现呢?通过以某种方式延迟对包含的评估?或者读取 inn-file 的内容并以某种方式评估字符串?或者是其他东西?

TIA,

仍在学习的史蒂夫

4

1 回答 1

1

只需将您想要的列表作为参数传递给您的外部 lambda。您的格式具有误导性,您可以尝试漂亮地打印您的代码。

(define write-out-techdisplays
  (lambda (techdisps)
    (for-each
      (lambda (rule)
        (begin
          (set! rule-string (map symbol->string rule))
          (print (string-join rule-string)))) ;inner-lambda closes here
      techdisps)))

(define alldisps 
  (call-with-input-file "./mytech/mytech.tf.techDisplays" 
                        read)))

;since the last arg is thunk the way to invoke it is to wrap
;our call to write-out-techdisplays with a lambda to set the correct list
(with-output-to-file out-file 
                     (lambda () 
                       (write-output-techdisplays alldisps)))

最后,您可以重新设计代码以在没有副作用的情况下工作(那些设置!s)。那 for-each 可能是一张地图。

于 2014-12-16T23:17:48.307 回答