Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheme operations within if condition

Generally in Scheme - syntax: (if test consequent alternate) .

I trying to do some operations within the consequent section , for example : if true then set sum1 on 44 and return 1 .
In Scheme code its -

(if #t ( 
          (set! sum1 44)
          (if #t 1)
          )) 

The above code doesn't works out and prompts -

application: not a procedure;
 expected a procedure that can be applied to arguments
  given: #<void>
  arguments...:
   1 
like image 827
URL87 Avatar asked Apr 26 '26 12:04

URL87


1 Answers

In Scheme, parentheses are always used for application; you can't add extra parentheses for grouping.

Your consequent here is:

((set! sum1 44) (if #t 1))

The outer pair of parentheses makes Scheme attempt to use the result of (set! sum1 44) as a procedure, applying it to the result of (if #t 1).

What (I think) you want is to evaluate the two expressions in sequence, and then return the result of the last one. The form to do this is begin, so it should be:

(begin (set! sum1 44) (if #t 1))
like image 70
John Bartholomew Avatar answered Apr 28 '26 06:04

John Bartholomew



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!