How do I get around this problem, I've been at it for a while.
It keeps giving me this error:
check-expect: expects 2 arguments, but found only 1
Its not counting the list as one argument by itself, I think that is the error, but how would I go about fixing this? I've tried doing (count-in cell (list cells)) but it then gives me an error saying define: expected a variable, but found a part
Definitions
(define-struct Cell (x y)
(define (count-in cell cells)
(cond [(member? cell cells) 1]
[else 0]))
Check-expect, here is where the error comes in
(check-expect (count-in (make-Cell 100 123)
(list
(make-Cell 104 123) (make-Cell 45 67))
(cond [(member? (make-Cell 100 123)
(list
(make-Cell 104 123) (make-Cell 45 67)))1]
[else 0])1)
This passes your test
(define-struct Cell (x y))
(define (count-in cell cells)
(cond
[(member? cell cells) 1]
[else 0]))
(check-expect
(count-in (make-Cell 100 123)
(list (make-Cell 104 123)
(make-Cell 45 67)))
(cond
[(member? (make-Cell 100 123) (list (make-Cell 104 123) (make-Cell 45 67))) 1]
[else 0]))
You should really learn how to indent Scheme code, and use the DrRacket's indentation feature and parenthesis coloring.
Related
I have this board [10,10] for this project below and I can't move the piece on the board
this question is part of the other questions about Lisp, you can see on my profile
(defun board ()
"T in position x=0 and y=0"
'(
(T 25 54 89 21 8 36 14 41 96)
(78 47 56 23 5 NIL 13 12 26 60)
(0 27 17 83 34 93 74 52 45 80)
(69 9 77 95 55 39 91 73 57 30)
(24 15 22 86 1 11 68 79 76 72)
(81 48 32 2 64 16 50 37 29 71)
(99 51 6 18 53 28 7 63 10 88)
(59 42 46 85 90 75 87 43 20 31)
(3 61 58 44 65 82 19 4 35 62)
(33 70 84 40 66 38 92 67 98 97)
)
)
not the same but similar here the rows begin at 1 but in project is start by 0
and this function to print the board
(defun print-board (board)
(format T "~%")
(mapcar (lambda (x) (format T " ~A ~%" x)) board)
(format nil ""))
I have 8 movements implemented but I only put 4 examples for the
question not to get too much code
(defun UP-LEFT (x y board)
"Function that receives 2 indexes and board, validate movement and move piece up and left"
(cond
((equal (validate-movements (- x 1) (- y 2) board) 0)
(move-piece x y -1 -2 board))
(T nil)))
(defun UP-RIGHT (x y board)
"receive 2 indexes and board, validate movement and move piece up and right"
(cond
((equal (validate-movements (+ x 1) (- y 2) board) 0)
(move-piece x y 1 -2 board))
(T nil)))
(defun LEFT-DOWN (x y board)
"Function that receives 2 indexes and board, validate movement and move piece left and down"
(cond
((equal (validate-movements (- x 2) (+ y 1) board) 0)
(move-piece x y -2 1 board))
(T nil)))
(defun LEFT-UP (x y board)
"Function that receives 2 indexes and board, validate movement and move piece left and up"
(cond
((equal (validate-movements (- x 2) (- y 1) board) 0)
(move-piece x y -2 -1 board))
(T nil)))
(defun DOWN-RIGHT (x y board)
"Function that receives 2 indexes and board, validate movement and move piece down and right"
(cond
((equal (validate-movements (+ x 1) (+ y 2) board) 0)
(move-piece x y 1 2 board))
(T nil)))
my doubt is in this move piece in board in axis (x,y)
(defun move-piece (x y dx dy board)
"Function that receives two indexes and board to move the piece on the board"
(mapcar
(lambda (L)
(cond
((atom L) L)
((and
(equal (nth 0 L) x)
(equal (nth 1 L) y))
(list (+ (nth 0 L) dx) (+ (nth 1 L) dy) (nth 2 L)
(nth 3 L) (nth 4 L) (nth 5 L) (nth 6 L)
(nth 7 L) (nth 8 L) (nth 9 L)))
(T L))) board))
and this function to validate movements
(defun validate-movements (x y board)
"Function that receives two indexes and board to validate movement"
(cond
((and
;; validation of rows and columns
(>= x 0)
(>= y 0)
(<= x 9)
(<= y 9)
(= (apply '+ (mapcar (lambda (L)
(cond
((atom L) 0)
((or (not(equal (nth 0 L ) x)) (not (equal (nth 1 L) y))) 0)
(T 1))) board)) 0)) 0)
(T nil )))
when I try to test the movements https://ideone.com/jaeCLu it's not move,
because don´t return nothing and show nothing
what I´m doing wrong?
Let's take a look at the validation function. First, make sensible limebreaks: when a multiline form is closed, break the line.
(defun validate-movements (x y board)
"Function that receives two indexes and board to validate movement"
(cond ((and
;; validation of rows and columns
(>= x 0)
(>= y 0)
(<= x 9)
(<= y 9)
(= (apply '+
(mapcar (lambda (L)
(cond ((atom L) 0)
((or (not (equal (nth 0 L ) x))
(not (equal (nth 1 L) y)))
0)
(T 1)))
board))
0))
0)
(T nil )))
A condition that has only two possible outcomes is better handled through if:
(defun validate-movements (x y board)
"Function that receives two indexes and board to validate movement"
(if (and
;; validation of rows and columns
(>= x 0)
(>= y 0)
(<= x 9)
(<= y 9)
(= (apply '+
(mapcar (lambda (L)
(cond ((atom L) 0)
((or (not (equal (nth 0 L ) x))
(not (equal (nth 1 L) y)))
0)
(T 1)))
board))
0))
0
nil))
Comparators like <= can take more arguments:
(defun validate-movements (x y board)
"Function that receives two indexes and board to validate movement"
(if (and (<= 0 x 9)
(<= 0 y 9)
(= (apply '+
(mapcar (lambda (L)
(cond ((atom L) 0)
((or (not (equal (nth 0 L) x))
(not (equal (nth 1 L) y)))
0)
(T 1)))
board))
0))
0
nil))
Since your board is a list of lists (one 10-element sublist per line), a line will never be an atom:
(defun validate-movements (x y board)
"Function that receives two indexes and board to validate movement"
(if (and (<= 0 x 9)
(<= 0 y 9)
(= (apply '+
(mapcar (lambda (L)
(cond ((or (not (equal (nth 0 L) x))
(not (equal (nth 1 L) y)))
0)
(T 1)))
board))
0))
0
nil))
Again, a two-clause conditional is better an if:
(defun validate-movements (x y board)
"Function that receives two indexes and board to validate movement"
(if (and (<= 0 x 9)
(<= 0 y 9)
(= (apply '+
(mapcar (lambda (L)
(if (or (not (equal (nth 0 L) x))
(not (equal (nth 1 L) y)))
0
1))
board))
0))
0
nil))
Now, I wanted to tell you how booleans are much easier to express logic with. However, that condition makes no sense to me: you seem to check that there is some line on the board that carries the x coordinate in its first field and the y coordinate in the second.
Maybe you wanted to check that the target coordinate is empty?
(defun target-valid-p (x y board)
(and (<= 0 x 9)
(<= 0 y 9)
(null (nth x (nth y board)))))
Next, the move function. Again, linebreaks:
(defun move-piece (x y dx dy board)
"Function that receives two indexes and board to move the piece on the board"
(mapcar (lambda (L)
(cond
((atom L) L)
((and (equal (nth 0 L) x)
(equal (nth 1 L) y))
(list (+ (nth 0 L) dx) (+ (nth 1 L) dy) (nth 2 L)
(nth 3 L) (nth 4 L) (nth 5 L) (nth 6 L)
(nth 7 L) (nth 8 L) (nth 9 L)))
(T L)))
board))
Your lines are never atoms:
(defun move-piece (x y dx dy board)
"Function that receives two indexes and board to move the piece on the board"
(mapcar (lambda (L)
(cond
((and (equal (nth 0 L) x)
(equal (nth 1 L) y))
(list (+ (nth 0 L) dx) (+ (nth 1 L) dy) (nth 2 L)
(nth 3 L) (nth 4 L) (nth 5 L) (nth 6 L)
(nth 7 L) (nth 8 L) (nth 9 L)))
(T L)))
board))
Two-branch conditional is if:
(defun move-piece (x y dx dy board)
"Function that receives two indexes and board to move the piece on the board"
(mapcar (lambda (L)
(if (and (equal (nth 0 L) x)
(equal (nth 1 L) y))
(list (+ (nth 0 L) dx) (+ (nth 1 L) dy) (nth 2 L)
(nth 3 L) (nth 4 L) (nth 5 L) (nth 6 L)
(nth 7 L) (nth 8 L) (nth 9 L))
L))
board))
Use list* and nthcdr to update part of a list:
(defun move-piece (x y dx dy board)
"Function that receives two indexes and board to move the piece on the board"
(mapcar (lambda (L)
(if (and (equal (nth 0 L) x)
(equal (nth 1 L) y))
(list* (+ (nth 0 L) dx)
(+ (nth 1 L) dy)
(nthcdr 2 L))
L))
board))
Now it seems that you again just update the first two cells of the line. Maybe I didn't understand your data model, but I would have thought that you just want to update the cells at the given coordinates:
(defun move-piece (x y dx dy board)
(let ((new-board (copy-tree board))
(new-x (+ x dx))
(new-y (+ y dy))
(piece (nth x (nth y board))))
(setf (nth x (nth y new-board)) nil
(nth new-x (nth new-y new-board)) piece)
new-board))
I have this board with atom T and I wanna get is position in list and sub-list
(defun board ()
"position of T: i=0 e j=9"
'(
;; 0 1 2 3 4 5 6 7 8 9
(96 25 54 89 21 8 36 14 41 T) ;; 0
(78 47 56 23 5 NIL 13 12 26 60) ;; 1
(0 27 17 83 34 93 74 52 45 80) ;; 2
(69 9 77 95 55 39 91 73 57 30) ;; 3
(24 15 22 86 1 11 68 79 76 72) ;; 4
(81 48 32 2 64 16 50 37 29 71) ;; 5
(99 51 6 18 53 28 7 63 10 88) ;; 6
(59 42 46 85 90 75 87 43 20 31) ;; 7
(3 61 58 44 65 82 19 4 35 62) ;; 8
(33 70 84 40 66 38 92 67 98 97);; 9
)
)
Function to get line and cell from board
(defun line (x board)
(nth x board))
(defun cell-board (x y board)
(nth y (line x board)))
(defun column (index board)
(cond ((not (numberp index)) nil)
((< index 0) nil)
(t (mapcar #'(lambda (line &aux (n-column (nth index line))) n-column) board))))
Function that receives the board and returns the position (i j) where the "T" is. If "T" is not on the board, NIL should be returned.
(defun find-T-position (board)
)
you can teste and see the result here https://ideone.com/GQIePI
(print "position : " (find-T-position (board)))
the result correct should be
(0 9)
The board function tries to call a literal list as-if it was a function. The quote is misplaced.
The find-t-position function has no body.
If you add more code and an actual question you will have better feedback.
Hint: either T is in current row (car board), or you need to search the board (cdr board); test often to spot errors.
(defun find-t (rows)
(let* ((col nil)
(row (position-if (lambda (r) (setf col (position t r))) rows)))
(values row col)))
Some tests:
[1]> (find-t nil)
NIL ;
NIL
[2]> (find-t '(()))
NIL ;
NIL
[3]> (find-t '((0)))
NIL ;
NIL
[4]> (find-t '((t)))
0 ;
0
[5]> (find-t '((0 t)))
0 ;
1
[6]> (find-t '((0 t 0)))
0 ;
1
[7]> (find-t '((0 0 t)))
0 ;
2
[8]> (find-t '((0 0 0)))
NIL ;
NIL
[9]> (find-t '((0 0 0)
(t 0 0)))
1 ;
0
[10]> (find-t '((0 0 0)
(t 0 t)))
1 ;
0
[11]> (find-t '((0 0 0)
(0 0 t)))
1 ;
2
[12]> (find-t '((0 0 t)
(0 0 t)))
0 ;
2
I find the answer in this question Lisp position of nested list element with children and it´s work perfectly
(defun my-position (elm tree &optional (start 0))
"find the generalized position of elm inside tree.
Parameters: elm - element to be found
tree - a list of atoms and lists in which to search the element
start - the tentative position"
(cond ((null tree) nil) ; element not present => nil
((atom (first tree)) ; if the first element is an atom, then
(if (eql elm (first tree)) ; if equal to element, found
(list start) ; return position start
;; otherwise, recur on rest of list incrementing the tentative position
(my-position elm (rest tree) (1+ start))))
;; otherwise, the first element is a list,
;; try to find it inside, with a recursive call
(t (let ((pos (my-position elm (first tree) 0)))
(if pos ; if not nil the element has been found
(cons start pos) ; return the current position followed by the position inside the list
; otherwise recur on rest of list incrementing the tentative position
(my-position elm (rest tree) (1+ start)))))))
and my function find-t-position just call the function my-position
with element 'T and the board and return the position of element 'T in
list
(defun find-T-position (board)
(my-position ('T board))
you can see the correct result https://ideone.com/DOIOoB
I have two functions in my program. The one that is commented out mods every element in a list by five. And the second function counts how many times an element appears in a list. How do I combine both of these to get my desired result of determining how many elements of a list are divisible by five?
Here is my code:
(defun divide-bye-five (lst)
(loop for x in lst collect (mod x 5)))
(defun counter (a lst)
(cond ((null lst) 0)
((equal a (car lst)) (+ 1 (counter a (cdr lst))))
(t (counter a (cdr lst)))))
(counter '0 '(0 0 0 20 0 0 0 0 0 5 31))
If you need just select all elemets in list, which are dividable by five, you can use remove-if-not.
(defun dividable-by-5 (num)
(zerop (mod num 5))
CL-USER> (remove-if-not #'dividable-by-5 '(1 2 3 10 15 30 31 40))
(10 15 30 40)
But I'm not sure, do you want select this elements, or just count them? Of course you can count them by calling length on resulting list, or of you don't need all elements, but just a number, you can use count-if.
CL-USER> (count-if #'dividable-by-5 '(1 2 3 10 15 30 31 40))
4
If you have two functions where the result of one is what you would like as the input for the second you can combine them like this:
(second-fun (first-fun first-fun-arg ...))
So specifically using your provided functions it should work doing:
(counter 0 (divide-bye-five '(1 2 3 4 5 6 7 8 9 10))) ; ==> 2
If you'd like to abstract it you make it a function:
(defun count-dividable-with-five (lst)
(counter 0 (divide-bye-five lst)))
Definitions:
(define-struct Cell (x y))
(define (count-in cell cells)
(cond
[(member? cell cells) 1]
[else 0]))
(define (touches cell cells)
(+
(count-in
(cell (Cell-x cell) (+ (Cell-y cell) 1)) (list cells))
(count-in
(cell (Cell-x cell) (+ (Cell-y cell) -1))(list cells))
(count-in
(cell (+ (Cell-x cell) -1) (Cell-y cell))(list cells))
(count-in
(cell (+ (Cell-x cell) 1) (Cell-y cell))(list cells))))
The check-expect and where the error occurs:
(check-expect (touches (make-Cell 2 30)
(list
make-Cell 2 31
make-Cell 1 29
make-Cell 1 30
make-Cell 2 30)) 2)
It is giving me an error saying:
function call: expected a function after the open parenthesis, but received (make-Cell 2 30)
Why is this happening? And how would I go about fixing this?
What the code does: Checks if a cell (with its values adjusted by 1) is presently in the list. So make-cell 2 30, the adjusting values would be
2 31, 2 29, 1 30, 3 30. And if the one of the cells is inside the list, itll produce a 1, if not it'll produce a 0. Then if the code just adds all them up telling me how many of the adjusted cells are in the list.
You forgot to surround the calls to make-cell with parentheses, and the touches procedure looks wrong. Assuming that the member? procedure is correctly implemented and works for a list of structures, this should work:
(define (touches cell cells)
(+
(count-in
(make-Cell (Cell-x cell) (+ (Cell-y cell) 1))
cells)
(count-in
(make-Cell (Cell-x cell) (+ (Cell-y cell) -1))
cells)
(count-in
(make-Cell (+ (Cell-x cell) -1) (Cell-y cell))
cells)
(count-in
(make-Cell (+ (Cell-x cell) 1) (Cell-y cell))
cells)))
(check-expect
(touches (make-Cell 2 30)
(list
(make-Cell 2 31)
(make-Cell 1 29)
(make-Cell 1 30)
(make-Cell 2 30)))
2)
If I do C-u M-: (to insert the result of a lisp statement into the buffer) and I then do something like:
(progn (setq x 0 l '()) (while(< x 30) (push (random 99) l) (incf x 1)) (nreverse l))
I get:
(89 29 27 23 56 88 37 11 33 20 98 95 ...)
With trailing ellipsis. What's a way around this? Something like buffer-insert across the resulting list.
try
(setq eval-expression-print-length nil)
in .emacs
M-:(insert (pp (loop repeat 30 collect (random 99))))
May need (require 'cl) first.