Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheme: if condition with multiple procedures

I am trying to figure out how to have my if statement do multiple tasks, but only return one thing, if that makes sense. Also, I have no clue how to print a string along with a variable in the same line.

for example, how would you do something like this in scheme (the following is java)

if(num < x){
  num++;
  x = 0;
  System.out.println("The value of x is " + x " and num is now" + num);
}

else System.out.println("error");

Here is what I tried:

(if (< num x) 
( (define num (+ 1 num))
   (define x 0)
   ;idk how to print it
   )
"error";else
)
like image 200
mr nooby noob Avatar asked Oct 30 '25 08:10

mr nooby noob


2 Answers

You can use begin to evaluate a series of expressions for effect, and return the last one. Also you can use printf to print:

(if (< num x)
    (begin
      (set! num (add1 num))
      (set! x 0)
      (printf "The value of x is ~a and num is now ~a\n" x num))
    "error")

However keep in mind that using set! to clobber (mutate) variables is discouraged in Racket. It's better to return new values. It's hard to show you how to do that, here, without a slightly bigger example and more context. Anyway if you're intersted that should probably be its own, new question here.

like image 190
Greg Hendershott Avatar answered Nov 01 '25 14:11

Greg Hendershott


I'll start off by saying I'm not exactly certain what you want your code to do because I don't know java.

Something I can say though is that the internal define expressions are definitely tripping you up. In racket you generally cannot define a global variable within an expression, but would rather create a local binding with a form like:

(let ([identifier expression]
        ...
        [id-n expression-n])
    body-expressions)

by which the body expressions are evaluated with the specified bindings, the last of which is returned as the result of the entire let expression. This is one way to do multiple things and return one result. Within the context of an (if test consequent alternative)

statement you could group multiple expressions together (eg as a consequent) using the (begin expression...)form, which executes all expressions and returns the result of the last expression. An alternative would be to use a cond expression, which has the form:

(cond [test conequents]
        ...
        [test-n cons-n]
        [else expression])

Each such test can have multiple consequents, so this might be clearer than using several begins.

Also, if you really want to mutate the value of num and x you would use the (set! id value) procedure, but this is unidiomatic for racket as a functionally recursive form would be preferred.

Going beyond these points requires some guesswork on my part but it looks like you are working within a function that takes two arguments, x and num, and want to increment num until it reaches the value of x, printing the values of x and num along the way, and if given an x value larger than num, you want to return an error

In racket you could do this recursively like so:

(define inc-print ;defining inc-print
  (lambda (x num) ;a function of two arguments
    (cond [(> num x) "error"]
          [(< num x)
           ;the printf command takes a string and prints it.
           ;you can add values to the string by inserting ~a's followed by as many
           ;expressions as ~a's
           ;\n is shorthand for (newline) so that subsequent calls to printf print on a newline
           (printf "The value of x is ~a and num is now ~a\n" x num)
           ;a recursive call to inc-print with num incremented by 1 and x the same
           (inc-print x (+ num 1))]
          [else num])))

example:

> (inc-print 5 2)
The value of x is 5 and num is now 2
The value of x is 5 and num is now 3
The value of x is 5 and num is now 4
5

Let me know if this answered your question!

like image 40
inordirection Avatar answered Nov 01 '25 13:11

inordirection



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!