reading in a top-level definition

82 Views Asked by At

Chicken Scheme 4.8.0.5

Greetings all,

Assuming I have a text file containing a top-level definition

topLevelDef.txt

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

and I bring it in as an include

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
)

So how can I achieve this? Either by delaying evaluation of the include somehow? Or reading in the contents of inn-file and evaluating the string somehow? Or something else?

TIA,

Still-learning Steve

1

There are 1 best solutions below

0
On

just pass the list you want as an argument to your outer lambda. your formatting is misleading, you could try to pretty print your code.

(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)))

finally, you could redesign your code to work without sideffects (those set!s). that for-each could be a map.