Reputation: 134
I am having some trouble in how to define no operation in scheme language
like ; in c
i want to do something like this:
(cond
[(null? exp) nop ]
.....
)
if i leave it empty it wiil return #t thanks!
Upvotes: 1
Views: 1836
Reputation: 17866
You should probably change your program to eliminate useless condition. But if you just need a nil value, the standard answer is to say (if #f #f)
, which is a one-legged if that is always false but has no false expression, hence returns nothing.
Upvotes: 0
Reputation: 1
Scheme has no statements, only expressions. Each expression returns a value -or may never return-
So you want an expression which does not compute much. You could use nil (or #f
, or any other value) for that purpose:
(cond
((null? exp) ())
....
)
If you wrote a condition with only a test - and no "then" body sub-expressions
(cond
((null? exp))
)
then the result of the cond
when exp
is nil is the result of the (null? exp)
test which is #t
actually, when exp
is null, you could just return exp
itself.
Upvotes: 1
Reputation: 29021
Note that functional programs are different to imperative programs. You should always think in every expression/function to return something (the result or value of that expression). With conditionals, you have to be careful to maintain this "something" through all the different branches, as your expression has to yield a value in any case.
Then, you have to decide what you want to return in that case, and build the code accordingly. If you don't want to return #t
, you can return just #f
or the empty list:
(cond
[(null? exp) #f]
.....
)
In fact, if you think carefuly, the concept of a "no op" (that is, do nothing), in C, is almost the same than "producing some value", because you're doing nothing apart from producing the value, that doesn't induce any change at all in your program.
Upvotes: 3