-1

I'm trying to call a subroutine from a function on scheme this what i have so far.

(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

this is the subroutine that im trying to call

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

)

but its not returning nothing.. please tell me what im doing wrong.

1 Answers1

0

In vababa you are using a unbound variable x. Perhaps you meant x1? It's not allowed to use define in procedure bodies that evaluates to values based on previous ones.

(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

You can do one of:

(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))
Sylwester
  • 47,942
  • 4
  • 47
  • 79