I am newbie to Scheme programming and trying to define a var in if condition. For example, I have:
(if (< x y) (define x y) ) ;(GOAL: if x < y, than x=y..)
But I got the error:
let: bad syntax (not an identifier and expression for a binding) in:...
Any ideas how to resolve this, would be greatly appreciated.
p.s. Sorry for my English
Using define is wrong; you are not defining a function here. There are two solutions:
Or
Unlike imperative languages you should refrain not use
define
orset!
to update variables where you can avoid it. In some circumstances it's needed, like in generators. Since you don't have a full code example where you'd like to do this I cannot see what obvious solution is to be used.The way to store intermediate values if by
let
or recursion:You can do several intermediates by
let*
You you can use recursion, where you update cur and lst in th innner procedure by recursion:
It is an error to
define
something that is already defined so thats why I definedIf you need to do this top level you can:
min_xy
. To alter a binding destructively (get it to reference another value) you can useset!
You'll alter the most local definition and it's an error if it doesnæt already exist. This can be used for creating generators: