GETTING "DELETING UNREACHABLE CODE ERROR" USING A DEFMACRO

Discussion of Common Lisp
Post Reply
joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

GETTING "DELETING UNREACHABLE CODE ERROR" USING A DEFMACRO

Post by joeish80829 » Mon May 26, 2014 4:02 am

Here is the macro:

Code: Select all

(defmacro print-mat (mat  &key to-file)
  
  `(cond (,to-file
	  (let ((*print-case* :downcase))
	    (with-open-file (str ,to-file
				 :direction :output
				 :if-exists :supersede
				 :if-does-not-exist :create)
	      (dotimes (i 4)
		(dotimes (j 2)
		  (format str "~a" (aref ,mat i j))
		  (format str " "))
		(format str "~%")))))

	 ((eq ,to-file nil)
	  (dotimes (i 4)
	    (dotimes (j 2)
	      (format t "~a" (aref ,mat i j))
	      (princ #\Space))
	    (princ #\Newline)))

	 (t nil)))

first I define a matrix like this:

Code: Select all

(defparameter a (make-array '(4 2) :initial-contents
             '((1 2)
              (3 4)
              (5 6)
              (7 8))))
;then I can print the matrix to a file at the repl, like this:

Code: Select all

(print-mat a :to-file "/home/w/quicklisp/dists/quicklisp/software/lisp-cv-master/data/data.txt")
The macro works as expected and prints the matrix to a file. If I call it in any function though, as below, I get a deleting unreachable code error. How can I fix my print-mat macro so I can call it in a function

Code: Select all

(defun x ()
(print-mat a :to-file "/home/w/quicklisp/dists/quicklisp/software/lisp-cv-master/data/data.txt"))

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: GETTING "DELETING UNREACHABLE CODE ERROR" USING A DEFMAC

Post by Goheeca » Mon May 26, 2014 4:54 am

I present my version without a macro involved:

Code: Select all

(defun 2d-array-to-list (array)
  (loop for i below (array-dimension array 0)
     collect (loop for j below (array-dimension array 1)
		collect (aref array i j))))

(defun print-mat (mat)
  (format t "~{~{~a~^~8t~}~^~%~}" (2d-array-to-list mat)))

(defun print-mat-to-file (mat file)
  (with-open-file (*standard-output* file
				     :direction :output
				     :if-exists :supersede
				     :if-does-not-exist :create)
    (print-mat mat)))
I've used 2d-array-to-list from here. I've used tabs instead of spaces as a cell delimiter.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: GETTING "DELETING UNREACHABLE CODE ERROR" USING A DEFMAC

Post by joeish80829 » Mon May 26, 2014 4:58 am

I really like mine that way..is there a way you can help me fix mine?

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: GETTING "DELETING UNREACHABLE CODE ERROR" USING A DEFMAC

Post by Goheeca » Mon May 26, 2014 7:33 am

Actually, I want to ask why are you using a macro in place, where a function should be used. Don't forget the rule where a function is sufficient don't create a macro.
So why have you chosen a macro? Is there an interesting context from that has emerged?
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: GETTING "DELETING UNREACHABLE CODE ERROR" USING A DEFMAC

Post by Goheeca » Mon May 26, 2014 7:43 am

By the way, I don't know why are you getting an error instead of a warning or a note. The thing is that you are expanding the macro into the body of a newly created function and only one certain branch of cond is reachable. Which also refers to a bad concept.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

joeish80829
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: GETTING "DELETING UNREACHABLE CODE ERROR" USING A DEFMAC

Post by joeish80829 » Mon May 26, 2014 9:12 am

Thanks for your help on this..I just decided to convert it to a defun and that solved it

Post Reply