【发布时间】:2016-06-29 13:56:53
【问题描述】:
问题
我和我的同事一直在尝试让一些 Scheme 代码工作,这些代码将从 TSV 文件中获取输入并将这些值分配给已在 Ansys 软件中定义的变量。
文件
TSV 文件的组织方式如下:
1 0.7 0.8 0.7 1.0 0.5 [...]
2 0.9 0.9 0.3 0.5 0.5 [...]
3 0.5 0.8 0.3 0.3 0.7 [...]
4 0.6 0.2 1.0 0.5 0.5 [...]
5 0.9 0.0 0.0 0.8 0.3 [...]
6 0.5 0.6 0.3 0.4 1.0 [...]
[ ... ]
1998 0.5 0.0 0.2 0.9 0.0 [...]
1999 0.8 0.2 0.2 0.7 0.2 [...]
2000 0.7 0.2 0.8 0.4 0.5 [...]
第一列是需要将特定输入集分配给软件中的变量的流动时间;其他 96 列代表需要设置的 96 个变量。
我目前的方案代码是这样的:
;Set variables
(define (setFunc)
(lambda(row-position outflow-number flow-wt-1)
(let outflow-string ((string-append("def bc outflow " outflow-number " ~a")))
(if (< row-position 180)
(ti-menu-load-string (format #f (outflow-string) flow-wt-1))
(ti-menu-load-string (format #f (outflow-string) 0.0))
)
)
)
)
;Read a file to a list of chars
(define (file->char_list)
(call-with-input-file "doorstates.txt"
(lambda(input-port)
(let loop ((x (read-char input-port)))
(if (not (eof-object? x))
(#t (begin (cons x (loop (read-char input-port)))))
)
)
)
)
)
;Main
(begin
(let inFile ((apply string (file->char_list)))
(let inFileList ((string-split (inFile #\newline)))
(let index ((0))
; Row position = flow-time - 1
(define rowPosition ((- flow-time 1)))
(let loop ((index))
(cond ((>= index (length inFileList))
'()
)
((= (- index (* (/ index 97) 97)) 0)
(+ index 1)
(loop (index))
)
((= (/ index 97) rowPosition)
; Column position = index - (rowPosition * rowLength)
(define colPosition ((- index (* rowPosition 97))))
(setFunc (rowPosition colPosition list-ref inFileList index))
(+ index 1)
(loop (index))
)
(else
(+ index 1)
(loop (index))
)
)
)
)
)
)
)
当上面的代码使用 Ansys 软件运行时,会产生以下错误:
Error: eval: unbound variable
Error Object: string
我读过的所有资料都说,未绑定变量的最可能原因是在不是函数的参数(如文字)周围放置了额外的括号;但是我所看到的所有内容都在 let 参数周围加上了双括号。
要点
在参与此项目之前,我从未使用过 Scheme 或任何其他 LISP 变体,因此如果有任何错误或我遗漏的常见做法,请告诉我。另外,如果上面的方案代码完全是废话,并且没有做我需要做的事情,请告诉我并建议下一步!
【问题讨论】: