如何在CHICKEN scheme脚本参数中使用数字

时间:2017-08-24 09:03:05

标签: scheme chicken-scheme

这是我的计划代码:

(define (fact n)
  (if (= n 1)
      1
      (* n (fact (- n 1)))))
(define (main args)
  (fact (car args)))

然后我在bash中使用此命令将此代码作为脚本运行:

csi -ss test.scm 4

但是,解释器会收到以下错误消息:

Error: (=) bad argument type: "4"

Call history:

<syntax>      (##core#lambda (n) (define (iter num result) (if (> num n) result (iter (+ num 1) (* result num)))) ...
<syntax>      [fact2] (##core#let ((iter (##core#undefined))) (##core#set! iter (##core#lambda (num result) (if (> num n) ...
<syntax>      [fact2] (##core#begin (##core#set! iter (##core#lambda (num result) (if (> num n) result (iter (+ num 1) (* ...
<syntax>      [fact2] (##core#set! iter (##core#lambda (num result) (if (> num n) result (iter (+ num 1) (* result num))))...
<syntax>      [fact2] (##core#lambda (num result) (if (> num n) result (iter (+ num 1) (* result num))))
<syntax>      [iter] (##core#begin (##core#if (> num n) result (iter (+ num 1) (* result num))))
<syntax>      [iter] (##core#if (> num n) result (iter (+ num 1) (* result num)))
<syntax>      [iter] (> num n)
<syntax>      [iter] (iter (+ num 1) (* result num))
<syntax>      [iter] (+ num 1)
<syntax>      [iter] (* result num)
<syntax>      [fact2] (iter 1 1)
<syntax>      [fact2] (##core#undefined)
<eval>    [main] (fact (car args))
<eval>    [main] (car args)
<eval>    [fact] (= n 1)    <--

我猜解释器将参数4作为字符串,那么如何在CHICKEN scheme脚本参数中使用数字?

1 个答案:

答案 0 :(得分:2)

尝试string->number,这是一个标准的R5RS程序:

(define (main args)
  (fact (string->number (car args))))