Tetris Rotation T piece Jess - jess

I am making a tetris game in Jess language and I have made this algorithm to rotate the T piece.
(defrule rotateTPiece
?g<-(piece (id 4) (x1 ?x1) (y1 ?y1)
(x2 ?x2) (y2 ?y2)
(x3 ?x3) (y3 ?y3)
(x4 ?x4) (y4 ?y4))
=>
(bind ?lowerx (min ?x1 ?x2 ?x3 ?x4))
(bind ?lowery (min ?y1 ?y2 ?y3 ?y4))
(modify ?g
(x1 (- ?x1 ?lowerx)) (y1 (- ?y1 ?lowery))
(x2 (- ?x2 ?lowerx)) (y2 (- ?y2 ?lowery))
(x3 (- ?x3 ?lowerx)) (y3 (- ?y3 ?lowery))
(x4 (- ?x4 ?lowerx)) (y4 (- ?y4 ?lowery)))
(modify ?g
(x1 ?y1) (y1 (- 1 (- ?x1 1)))
(x2 ?y2) (y2 (- 1 (- ?x2 1)))
(x3 ?y3) (y3 (- 1 (- ?x3 1)))
(x4 ?y4) (y4 (- 1 (- ?x4 1))))
(modify ?g
(x1 (+ ?x1 ?lowerx)) (y1 (+ ?y1 ?lowery))
(x2 (+ ?x2 ?lowerx)) (y2 (+ ?y2 ?lowery))
(x3 (+ ?x3 ?lowerx)) (y3 (+ ?y3 ?lowery))
(x4 (+ ?x4 ?lowerx)) (y4 (+ ?y4 ?lowery)))
(focus VISUAL))
In first modify I translate the piece to the origin, then I modify it to make a rotation and then I translate it to the inicial position. I don't know why but this is not working.

Slot bindings are by value, so ?x1 refers to the value in slot x1 at the time the right hand side is entered. Replace the first and second modify, using the pattern
(bind ?tx1 (- ?x1 ?lowerx))
and
(bind ?ux1 ?tx1)
and the last modify becomes
(modify ?g
(x1 (+ ?ux1 ?lowerx)) ... )

Related

Totally confused by racket syntax - please lend a hand

I am being asked to learn some racket for university and will eventually have to do some pretty complicated stuff with tree structures and data sets. I have just started and I cannot understand how this code works even with the Racket docs, google and SO.
I'm trying to write a function that takes three number arguments and returns the greatest, here's what I have:
(define (mymax x1 x2 x3)
(cond
((and (x1 > x2) (x1 > x3)) x1)
(else (and (x2 > x1) (x2 > x3)) x2)
(else (and (x3 > x1) (x3 > x2)) x3)
))
(print (mymax 10 5 1))
So...
I don't know if I'm using the and comparison correctly
I'm getting the error "cond: bad syntax (`else' clause must be last) in: (else (and (x2 > x1) (x2 > x3)) x2)"
I don't really have any idea how to return a value from a function
Sorry to be so clueless but this is just not making sense, any help shedding light on these bullet points would be a massive help
Here is the syntax for a conditional expression:
(cond
[ConditionExpression1 ResultExpression1]
[ConditionExpression2 ResultExpression2]
...
[ConditionExpressionN ResultExpressionN])
(cond
[ConditionExpression1 ResultExpression1]
[ConditionExpression2 ResultExpression2]
...
[else DefaultResultExpression])
The evaluation of a conditional expression follows 2 rules
1) rule cond_false
(cond == (cond
[#false ...] ; first line removed
[condition2 answer2] [condition2 answer2]
...) ...)
2) rule cond_true
(cond == answer-1
[#true answer-1]
[condition2 answer2]
...)
This second rule also applies when the condition is else (but note that else can only occur in the last clause).
Example:
(cond
[(= 2 0) #false]
[(> 2 1) (string=? "a" "a")]
[else (= (/ 1 2) 9)])
== {by evaluation of (= 2 0) to #false}
(cond
[#false #false]
[(> 2 1) (string=? "a" "a")]
[else (= (/ 1 2) 9)])
== {by rule cond_false}
(cond
[(> 2 1) (string=? "a" "a")]
[else (= (/ 1 2) 9)])
== {by evaluation of (> 2 1) to #true}
(cond
[#true (string=? "a" "a")]
[else (= (/ 1 2) 9)])
== {by rule cond_true}
(string=? "a" "a")
== {by evaluation of (string=? "a" "a") to true}
#true
(define (mymax x1 x2 x3)
(cond
((and (x1 > x2) (x1 > x3)) x1)
(else (and (x2 > x1) (x2 > x3)) x2)
(else (and (x3 > x1) (x3 > x2)) x3)
))
(print (mymax 10 5 1))
an expression like (2 > 1) would not work. It should be (> 2 1). The syntax for function application is a prefix syntax i.e. the open parenthesis should be followed by the function name which should be followed by the arguments.
the error you get is for bad syntax for the second clause. (else (and (x2 > x1) (x2 > x3)) x2) This clause has 3 pieces: else, (and (x2 > x1) (x2 > x3)), and x2. However, according to the syntax of cond, a clause should only have 2 pieces.
After getting rid of the elses and making the > prefix:
(define (mymax x1 x2 x3)
(cond
((and (> x1 x2) (> x1 x3)) x1)
((and (> x2 x1) (> x2 x3)) x2)
((and (> x3 x1) (> x3 x2)) x3)))
(print (mymax 10 5 1))
The program prints 10. But note that it wouldn't work for (mymax 5 5 5), so we turn all the >s to >=s:
(define (mymax x1 x2 x3)
(cond
[(and (>= x1 x2) (>= x1 x3)) x1]
[(and (>= x2 x1) (>= x2 x3)) x2]
[(and (>= x3 x1) (>= x3 x2)) x3]))
(mymax 10 5 1)
; => 10
(mymax 5 5 5)
; => 5
Lastly, functions don't "return" values. A better mental model would be to think that their body reduces to a value.
(define (f x-1 ... x-n)
f-body)
(f v-1 ... v-n)
; == f-body
; with all occurrences of x-1 ... x-n
; replaced with v-1 ... v-n, respectively
See: Racket Guide, The Racket Reference, HtDP.

Why is the parameter for my procedure getting a 'not a function' error?

I am going through the book Structure and Interpretation of Computer Programming, which uses Scheme, and I just got through a portion on recursion. I wrote a program for exercise 1.11:
A function f is defined by the rule that f(n) = n if n<3 and f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n> 3. Write a procedure that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative process.
I wrote the code at repl.it and when I run the procdure with the input 2, it gives me the error: Error: 2 is not a function [fRecurse, (anon)]. Can somebody explain to me what this means and how I can fix it? Why is it expecting my input to be a function?
Code:
(define (fRecurse n)(
(cond ((< n 3) n)
((>= n 3)
(+ (procRecurse (- n 1))
(* 2 (f (- fRecurse 2)))
(* 3 (f (- fRecurse 3))))))))
(fRecurse 2)
The error is due to an extra pair of parentheses before (cond...). To fix the issue, we simply remove the extra pair of parentheses:
(define (fRecurse n)
(cond ((< n 3) n)
((>= n 3)
(+ (fRecurse (- n 1))
(* 2 (fRecurse (- n 2)))
(* 3 (fRecurse (- n 3)))))))
(fRecurse 2)
And some additonal fixes I made to your example to make it work correctly:
Change f to fRecurse
Change procRecurse to fRecurse
Change (* 2 (f (- fRecurse 2))) to (* 2 (fRecurse (- n 2)))
Change (* 3 (f (- fRecurse 3))) to (* 3 (fRecurse (- n 3)))
See the updated repl.it

What this functions in Scheme language do?

I'm a newbie and I didn't understand very well the language. Could anyone please explain to me what this functions do?
First function:
(define (x l)
(cond
((null? l) 0)
((list? (car l))
(+ (x (car l)) (x (cdr l))))
(else (+ 1 (x (cdr l))))
))
Second function:
(define (x l)
(cond
((null? l) 0)
((list? (car l))
(+ (x (car l)) (x (cdr l))))
(else (+ (car l) (x (cdr l)))
))
I do understand the begining but the conditions I didn't understand. Any help?
I will call your second function y.
Writing in pseudocode,
x [] -> 0
x [a . b] -> x a + x b , if list a
x [a . b] -> 1 + x b , else, i.e. if not (list a)
y [] -> 0
y [a . b] -> y a + y b , if list a
y [a . b] -> a + y b , else, i.e. if not (list a)
So for example,
x [2,3] = x [2 . [3]]
= 1 + x [3]
= 1 + x [3 . []]
= 1 + (1 + x [])
= 1 + (1 + 0 )
and
y [2,3] = y [2 . [3]]
= 2 + y [3]
= 2 + y [3 . []]
= 2 + ( 3 + y [])
= 2 + ( 3 + 0 )
See? The first counts something in the argument list, the second sums them up.
Of course both functions could be called with some non-list, but then both would just cause an error trying to get (car l) in the second clause, (list? (car l)).
You might have noticed that the two are almost identical. They both accumulates (fold) over a tree. Both of them will evaluate to 0 on the empty tree and both of them will sum the result of the same procedure on the car and cdr when the car is a list?. The two differ when the car is not a list and in the first it adds 1 for each element in the other it uses the element itself in the addition. It's possible to write the same a little more compact like this:
(define (sum l)
(cond
((null? l) 0) ; null-value
((not (pair? l)) l) ; term
(else (+ (sum (car l)) (sum (cdr l)))))) ; combine
Here is a generalisation:
(define (accumulate-tree tree term combiner null-value)
(let rec ((tree tree))
(cond ((null? tree) null-value)
((not (pair? tree)) (term tree))
(else (combiner (rec (car tree))
(rec (cdr tree)))))))
You can make both of your procedures in terms of accumulate-tree:
(define (count tree)
(accumulate-tree tree (lambda (x) 1) + 0))
(define (sum tree)
(accumulate-tree tree (lambda (x) x) + 0))
Of course you can make a lot more than this with accumulate-tree. It doesn't have to turn into an atomic value.
(define (double tree)
(accumulate-tree tree (lambda (x) (* 2 x)) cons '()))
(double '(1 2 ((3 4) 2 3) 4 5)) ; ==> (2 4 ((6 8) 4 6) 8 10)

change - to + in Common Lisp

Is there way to change - (minus) function to + (plus) function?
My homework is to implement sin calculation on Macluaurin series
sin(x) = x-(x^3/3!)+(x^5/5!) -(x^7/7!)+(x^9/9!)-...
Each article has different sign. This is my Lisp code
(defun sinMac (x series n plusminus)
(cond ((= series 0) 0)
(t (funcall plusminus
(/ (power x n) (factorial n))
(sinMac x (- series 1) (+ n 2) plusminus)))))
Is it possible to change plusminus to exchange sign? if I get '+ function send '- to next recursive call. From that call (got '-) I call '+ and so on...
You could do it with a circular list. Like so:
(defun sin-mac (x series n plus-minus)
(cond ((zerop series) 0)
(t (funcall (car plus-minus)
(/ (power x n) (factorial n))
(sin-mac x (1- series) (+ n 2) (cdr plus-minus))))))
(sin-mac x series 1 '#0=(+ - . #0#))
Or even better, wrap up the initial arguments using labels:
(defun sin-mac (x series)
(labels ((recur (series n plus-minus)
(cond ((zerop series) 0)
(t (funcall (car plus-minus)
(/ (power x n) (factorial n))
(recur (1- series) (+ n 2) (cdr plus-minus)))))))
(recur series 1 '#0=(+ - . #0#))))
If the function is a symbol, this is easy:
(defun next-function (function)
(ecase function
(+ '-)
(- '+)))
(defun sinMac (x series n plusminus)
(cond ((= series 0) 0)
(t (funcall plusminus
(/ (power x n) (factorial n))
(sinMac x
(- series 1)
(+ n 2)
(next-function plusminus))))))
I would not swap the function but just the sign. Using a loop for this also seems clearer to me (and is most likely more efficient, although there is still plenty of opportunity for optimization):
(defun maclaurin-sinus (x n)
"Calculates the sinus of x by the Maclaurin series of n elements."
(loop :for i :below n
:for sign := 1 :then (- sign)
:sum (let ((f (1+ (* 2 i))))
(* sign
(/ (expt x f)
(factorial f))))))
A few optimizations make this about 10 times faster (tested with n = 5):
(defun maclaurin-sinus-optimized (x n)
"Calculates the sinus of x by the Maclaurin series of n elements."
(declare (integer n))
(loop :repeat n
:for j :from 0 :by 2
:for k :from 1 :by 2
:for sign := 1 :then (- sign)
:for e := x :then (* e x x)
:for f := 1 :then (* f j k)
:sum (/ e f sign)))

Scheme - define variable as the result of a function?

The beginning of one of my programs results in an error. This is the problem area. I am trying to define a variable as the result of a recursive function.
(define (test n)
(define (a1func i)
(if (= i 1) 0
(+ (/ 1 i) (a1func (- i 1)))))
(define a1 (a1func (- n 1))))
if you were to give it say (test 10) the error would be:
procedure application: expected procedure, given: #<undefined>; arguments were: 9
I assumed this could be done in Scheme?? ideas?
In pure FP languages computations are done passing parameters to functions, which return some values as a result. You could bind the result of test in the function which called test:
(define (test n)
(define (a1func i)
(if (= i 1) 0
(+ (/ 1 i) (a1func (- i 1)))))
(a1func (- n 1)))
(define (calltest x)
(define (r (test (+ 2 x))))
(- r 4))
Variables usually bound once and cannot be changed. A function must return the value, a result of expression, but (define a1 (a1func(- n 1))) is rather a definition, not an expression, so the correct code would be:
(define (test n)
(define (a1func i)
(if (= i 1) 0
(+(/ 1 i) (a1func(- i 1)))))
(define a1 (a1func(- n 1)))
a1)
And since defining variable and immediate returning it is meaningless, a more correct code would be:
(define (test n)
(define (a1func i)
(if (= i 1) 0
(+(/ 1 i) (a1func(- i 1)))))
(a1func(- n 1)))
If your scheme implementation support lisp macros then you can write this:
(define-macro (test n)
(define (a1func i)
(if (= i 1) 0
(+ (/ 1 i) (a1func (- i 1)))))
`(define a1 ,(a1func (- n 1))))
or using named let
(define-macro (test n)
`(define a1 ,(let a1func ((i (- n 1)))
(if (= i 1)
0
(+ (/ 1 i) (a1func (- i 1)))))))