Reputation: 11022
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
Upvotes: 2
Views: 705
Reputation: 236034
Some interpreters (Racket, for instance) complain if you write an if
without an else
part. For dealing with this, and for avoiding the need to explicitly use a begin
when more than one expression goes in the consequent part, it's better to use the when
special form (if available, because it's non-standard):
(when #t
(set! sum1 44)
(when #t 1))
In general, this is the structure of a when
and its sibling unless
:
(when <condition> ; equivalent to (if <condition> (begin ...))
<exp1> ; consequent, no alternative
<exp2>)
(unless <condition> ; equivalent to (if (not <condition>) (begin ...))
<exp1> ; consequent, no alternative
<exp2>)
If both the consequent and the alternative have more than one expression, you could use if
with a begin
in each part:
(if <condition>
(begin ; consequent
<exp1>
<exp2>)
(begin ; alternative
<exp3>
<exp4>))
... But it'd be more practical to use a cond
, which implicitly uses a begin
in each of its clauses:
(cond (<condition>
<exp1> ; consequent
<exp2>)
(else ; alternative
<exp3>
<exp4>))
Upvotes: 3
Reputation: 6596
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))
Upvotes: 4