dimanche 30 août 2020

If-else statement error in Scheme using guile

Total newbie to Scheme here.

I've been stuck on a scheme problem for sometime now. I don't understand how to code this right. I've looked every where on this site and others, and I just can't get this to work.

the problem: define a function Square that squares its parameters. If the parameter is not a number, print the message "invalid_input".

Here is what I have tried:

(define (square x) (cond
                      ((number? x) (* x x))
                      (else (display "invalid_input\n"))
                   )
)

I've also tried this:

 (define (square x) (cond
                       ((number? x) (* x x))
                       ((not (number? x)) (display "invalid_input\n"))
                    )
 )

And this:

(define (square x) (if 
                   (number? x) (* x x) (display "invalid_input\n")
                   )
)

None of these have worked when I call square like this (square h). I keep getting this error on Linux

scheme@(guile-user)> (square h)
;;; <stdin>:44:0: warning: possibly unbound variable `h'
<unnamed port>:44:0: In procedure #<procedure 7fcc7d0a0b60 at <current input>:44:0 ()>:
<unnamed port>:44:0: In procedure module-lookup: Unbound variable: h
Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.

Shouldn't it print "invalid_input" since 'h' is not a number? help me out here please. thank you

Aucun commentaire:

Enregistrer un commentaire