Nested while loop in Scheme with Gimp? - scheme

I'm writing a Gimp Script-Fu script, and trying to use a nested while loop. x is set to 15, y is set to 30. y loops up to 35, yet x stays at 15 and the loop quits. What is wrong here? Why is the value of x not changed?
(while (< x 20)
(while (< y 35)
(gimp-message (string-append (number->string x) "-" (number->string y)))
(set! y (+ y 1)))
(set! x (+ x 1)))

y is never being reset back to 0. Your code will increment y up to 35, then increment x 20 times, however on each subsequent increment of x y is still set to 35.
If you wanted to go over each combination of values of x and y then you would need code more like this:
(while (< x 20)
(set! y 0)
(while (< y 35)
(gimp-message (string-append (number->string x) "-" (number->string y)))
(set! y (+ y 1))
)
(set! x (+ x 1))
)
Here is a more complete example now that I've had time to work through this question with Gimp (I'm using print instead of gimp-message because I'm working in the console, but it should be interchangeable). To start I'm defining a function called SO that accepts the arguments, x, y that both represents pairs of min and max values:
(define (SO x y)
(let* ((x! (car x)) (y! (car y)))
(while (< x! (car (cdr x)))
(set! y! (car y))
(while (< y! (car (cdr y)))
(print (string-append (number->string x!) "-" (number->string y!)))
(set! y! (+ y! 1))
)
(set! x! (+ x! 1))
)
)
)
Inside this function, I'm pulling out the first and last values of x and y (with (car x) and (car (cdr x)) then I'm using let* to create two inner variables calledx!andy!that I will be altering the value of (to remove side effects of havingxandy` change after the function is called). If you call this function like so:
(SO '(15 20) '(30 35))
You get the following output:
"15-30"
"15-31"
"15-32"
"15-33"
"15-34"
"16-30"
"16-31"
"16-32"
"16-33"
"16-34"
"17-30"
"17-31"
"17-32"
"17-33"
"17-34"
"18-30"
"18-31"
"18-32"
"18-33"
"18-34"
"19-30"
"19-31"
"19-32"
"19-33"
"19-34"

Related

Scheme: Not a procedure (Dr. Racket)

I'm running this program in Dr. Racket using R5RS scheme and am getting this error on the line (+ 1 IntDivide((- x y) y)):
"application: not a procedure; expected a procedure that can be
applied to arguments given: 5 arguments...:"
The procedure is supposed to return the quotient of the division between two integers using subtraction. Since this is a homework problem, I'm not going to ask whether my solution is correct (I can debug that later), but rather what is causing this error. It seems to be commonly caused by excess brackets, but I can't seem to find them. Any help would be appreciated.
(define IntDivide (lambda (x y)
(if (eqv? (integer? x) (integer? y))
(begin
(if (= y 0)
(begin
(write "Can't divide by zero") (newline)
-1
)
)
(if (= (- x y) 0)
1
)
(if (< x y)
0
)
(if (> x y)
(+ 1 IntDivide((- x y) y))
)
)
)
(write "Please only input integers")
))
Thanks in advance!
In addition to moving the operator inside the parens, you also need to replace the if with a cond:
(define IntDivide
(lambda (x y)
(if (eqv? (integer? x) (integer? y))
(cond ((= y 0) (write "Can't divide by zero")
(newline)
-1)
((= x y) 1)
((< x y) 0)
((> x y) (+ 1 (IntDivide (- x y) y))))
(write "Please only input integers"))))
The way you have it now, with the interior if expressions, won't work because they don't automatically return. They just evaluate and then the result gets thrown away.
Call IntDivide the same way you would any other function.
(+ 1 (IntDivide (- x y) y))

Not sure if I understand anonymous procedures

(define (map2 liste1 liste2)
(define (gj x y)
(/ (+ x y) 2))
(if (or (null? liste1) (null? liste2))
'()
(cons (gj (car liste1) (car liste2)) (map2 (cdr liste1) (cdr liste2)))))
Is procedure gj an anonymous procedure since it's within another procedure?
gj is not anonymous since it has a name, which happens to be visible only within the scope of map2.
Examples of anonymous procedures would be:
> ((lambda (x) (* 2 x)) 10)
^^^^^^^^^^^^^^^^^^^^
20
or
> (map (lambda (x) (+ x 1)) '(10 20 30))
^^^^^^^^^^^^^^^^^^^^
'(11 21 31)
which have no name and cannot be referred to after the expression in which they are defined.
Note that
(define (gj x y)
(/ (+ x y) 2))
is the same as
(define gj
(lambda (x y)
(/ (+ x y) 2)))
so here the procedure is bound to identifier gj and therefore it's no longer anonymous.

Recursive function not working as planned

I am writing a function in Scheme that is supposed to take two integers, X and Y, and then recursively add X/Y + (X-1)/(Y-1) + ...until one of the numbers reaches 0.
For example, take 4 and 3:
4/3 + 3/2 + 2/1 = 29/6
Here is my function which is not working correctly:
(define changingFractions (lambda (X Y)
(cond
( ((> X 0) and (> Y 0)) (+ (/ X Y) (changingFunctions((- X 1) (- Y 1)))))
( ((= X 0) or (= Y 0)) 0)
)
))
EDIT: I have altered my code to fix the problem listed in the comments, as well as changing the location of or and and.
(define changingFractions (lambda (X Y)
(cond
( (and (> X 0) (> Y 0)) (+ (/ X Y) (changingFunctions (- X 1) (- Y 1) )))
( (or (= X 0) (= Y 0)) 0)
)
))
Unfortunately, I am still getting an error.
A couple of problems there:
You should define a function with the syntax (define (func-name arg1 arg2 ...) func-body), rather than assigning a lambda function to a variable.
The and and or are used like functions, by having them as the first element in a form ((and x y) rather than (x and y)). Not by having them between the arguments.
You have an extra set of parens around the function parameters for the recursive call, and you wrote changingFunctions when the name is changingFractions.
Not an error, but don't put closing parens on their own line.
The naming convention in Lisps is to use dashes, not camelcase (changing-fractions rather than changingFractions).
With those fixed:
(define (changing-fractions x y)
(cond
((and (> x 0) (> y 0)) (+ (/ x y) (changing-fractions (- x 1) (- y 1))))
((or (= x 0) (= y 0)) 0)))
But you could change the cond to an if to make it clearer:
(define (changing-fractions x y)
(if (and (> x 0) (> y 0))
(+ (/ x y) (changing-fractions (- x 1) (- y 1)))
0))
I personally like this implementation. It has a proper tail call unlike the other answers provided here.
(define (changing-fractions x y (z 0))
(cond ((zero? x) z)
((zero? y) z)
(else (changing-fractions (sub1 x) (sub1 y) (+ z (/ x y))))))
(changing-fractions 4 3) ; => 4 5/6
The trick is the optional z parameter that defaults to 0. Using this accumulator, we can iteratively build up the fractional sum each time changing-fractions recurses. Compare this to the additional stack frames that are added for each recursion in #jkliski's answer
; changing-fractions not in tail position...
(+ (/ x y) (changing-fractions (- x 1) (- y 1)))

Drawing table/board in Racket

I'm trying to create a game whose name is "Same" in Racket 5.0.2 version.
Here is explanation of the game:
http://download.racket-lang.org/docs/5.0.2/html/games/same.html?q=games
I created a row and draw it:
a: width
b: height
r: radius
(define (color x) ///for random colors
(cond [(< (random x) 100) 'blue]
[(< (random x) 200) 'purple]
[(< (random x) 300) 'yellow]
[(< (random x) 400) 'red]
[else 'green]))
(define-struct top (coord color))
(define (row x y)
(if (> x (- a r)) empty
(cons (make-top (make-posn x y)(color 500)) (row (+ x (* 2 r)) y))))
(define (draw-row L)
(if (empty? L) #f
(and
(draw-solid-disk (top-coord (first L)) r (top-color (first L)))
(draw-row (rest L)))))
So I've got a top row included 20 disks with random colors. But I need 200 disks in 20 rows and 10 columns. So I created a board like this:
(define (board x y)
(if (> y (- b r)) empty
(cons (row x y) (board x (+ y (* 2 r))))))
But I couldn't draw it. I tried to create a function as "draw-row" but I got error.
So my question is: How can I draw this board?
You can draw a row, and a board is just a list of rows, so I would expect
(define (draw-board b)
(for-each draw-row b))
to Just Work.
Based on your comment, an example I would expect to work:
(draw-board (board 10 10))
and it does for me, at least.

Scheme how to define var in if condition

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
Unlike imperative languages you should refrain not use define or set! 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:
;; within the let block x shadows the original x
;; with the smalles of the outer x and y
(let ((x (if (< x y) x y)))
(do-something x))
You can do several intermediates by let*
(let* ((tmp (+ x y))
(tmp2 (* tmp y))) ; tmp is bound here
(do-something-with tmp2)); or tmp and tmp2
You you can use recursion, where you update cur and lst in th innner procedure by recursion:
(define (mmin x . xs)
(define (min-aux cur lst)
(cond ((null? lst) cur)
((<= cur (car lst)) (min-aux cur (cdr lst)))
(else (min-aux (car lst) (cdr lst)))))
(min-aux x xs)) ; start recursion
It is an error to define something that is already defined so thats why I defined
If you need to do this top level you can:
(define min_xy (if (< x y) x y))
min_xy. To alter a binding destructively (get it to reference another value) you can use set!
(set! x (+ x 1)) ; increases x
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:
(define (generator start alter-proc)
(lambda () ; returns a procedure taking 0 arguments
(let ((old start)) ; temporary store what start points to
(set! start (alter-proc start)) ; change what the local start points to
old))) ; return the old value
(define counter (generator 1 (lambda (x) (+ x 1))))
(counter) ; ==> 1
(counter) ; ==> 2
(define doubler (generator 1 (lambda (x) (* x 2))))
(doubler) ; ==> 1
(doubler) ; ==> 2
(doubler) ; ==> 4
Using define is wrong; you are not defining a function here. There are two solutions:
(if (< x y) (set! x y) (void)) ; if x < y set x = y; else do nothing
Or
(set! x (if (< x y) y x)) ; sets x to y if (x<y) is true; else sets x to x

Resources