阅读顶级定义

时间:2014-12-16 00:14:41

标签: chicken-scheme

Chicken Scheme 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  )
  )
)

我把它作为一个包含

trythis.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文件的内容并以某种方式评估字符串?或其他什么?

TIA,

仍在学习史蒂夫

1 个答案:

答案 0 :(得分: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)))

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