在方案中调用子程序

时间:2015-04-09 21:54:44

标签: scheme subroutine

我正试图从我迄今为止的方案函数中调用一个子程序。

(define (main)
(display "Ingrese un parametro: ")
(define x (read-line))
(newline)
(display "Ingrese una posicion: ")
(define dig (read))
(newline)
(if (string? x)
    (begin  
        (if (number? dig)

        (begin 
        ;(vababa x dig))) <---calling subrutine

这是我试图调用的子程序

define (vababa x1 dig1)    
    (define a (string-pad-right x dig1))
    (define b (string-pad-left x dig1))
    (define z (string-append a b))
    z

但它没有返回任何内容..请告诉我我做错了什么。

1 个答案:

答案 0 :(得分:0)

vababa中,您使用的是未绑定的变量x。也许你的意思是x1? 不允许在过程体中使用define来评估基于先前值的值。

(define (vababa x1 dig1)    
    (define a (string-pad-right x dig1)) ; x does not exist anywhere
    (define b (string-pad-left x dig1))  ; x does not exist anywhere
    (define z (string-append a b)) ; a and b does not exist yet
    z) ; now a and b exists but you won't get there since `string-append` didn't get string arguments

您可以执行以下操作之一:

(define (vababa x1 dig1)    
  (string-append (string-pad-right x1 dig1)  ; replaced x with x1
                 (string-pad-left x1 dig1))) ; replaced x with x1


(define (vababa x1 dig1)    
  (define a (string-pad-right x1 dig1)) ; replaced x with x1
  (define b (string-pad-left x1 dig1))  ; replaced x with x1
  ;; in the body a and b exists
  (string-append a b))


(define (vababa x1 dig1)    
  (let ((a (string-pad-right x1 dig1)) ; replaced x with x1
        (b (string-pad-left x1 dig1))) ; replaced x with x1
    (string-append a b)))


(define (vababa x1 dig1)    
  (let* ((a (string-pad-right x1 dig1)) ; replaced x with x1
         (b (string-pad-left x1 dig1))  ; replaced x with x1
         (z (string-append a b)))
    z))