From: Barry Margolin
Subject: Re: Emacs Lisp print problem
Date: 
Message-ID: <barmar-F01765.09073417022006@comcast.dca.giganews.com>
In article <························@g44g2000cwa.googlegroups.com>,
 ·····@stevedurkin.net wrote:

> Can anyone tell me why the following line generates an error (but still
> prints the correct output!)?  It's a simple finite automata simulator;
> str is a list of integers (ASCII values); and the following line occurs
> when the recursive FA function encounters a character from outside of
> the alphabet {a, b} (or {97, 98} if you prefer):
> 
> (princ (format "%s unrecognized input\nrejected\n" (char-to-string (car
> str))) lisp-out))
> 
> Wrong type argument: integer-or-marker-p, "y unrecognized
> input^Jrejected^J"

princ returns its first argument after printing it, so FA is returning 
the error message.  Your accept function passes the return value to %, 
but this isn't valid for strings.  So either you need better error 
checking of the return value, or you need to ensure that FA always 
returns a state, even in the error case.

> 
> Here is the whole program if necessary:
> 
> ;Assignment: Finite Automata Simulator
> 
> (cond ((get-buffer "*lisp-out*") (kill-buffer "*lisp-out*")))
> (setq lisp-out (get-buffer-create "*lisp-out*"))
> 
> (defun FA (str)
>  (cond
>   ((null str) state)
>   ((equal (car str) 97)
>    (cond
>      ((equal state 1) (setq state 3))
>      ((equal state 2) (setq state 4))
>      ((equal state 3) (setq state 4))
>      ((equal state 4) (setq state 5))
>      ((equal state 5) (setq state 2))
>    )
>    (princ (format "a - > State %d\n" state) lisp-out)
>    (setq i (+ i 1))
>    (FA (cdr str))
>   )
>   ((equal (car str) 98)
>    (cond
>      ((equal state 1) (setq state 5))
>      ((equal state 2) (setq state 1))
>      ((equal state 3) (setq state 2))
>      ((equal state 4) (setq state 4))
>      ((equal state 5) (setq state 3))
>    )
>    (princ (format "b - > State %d\n" state) lisp-out)
>    (setq i (+ i 1))
>    (FA (cdr str))
>   )
>   (t (princ (format "%s unrecognized input\nrejected\n" (char-to-string
> (car str))) lisp-out))
>  )
> )
> 
> (defun accept (state)
>   (cond
>    ((equal (% state 2) 1) 't)
>    (t 'f)
>   )
> )
> 
> (setq keepgoing 't)
> (while keepgoing
>   (setq s (read-string "String:  "))
>   (princ (format "String:  %s\n" s) lisp-out)
>   (setq s (mapcar 'identity s))
>   (cond
>    ((equal s '(101 110 100))
>      (princ (format "bye\n") lisp-out)
>      (setq keepgoing nil)
>    )
>    (t (princ (format "Start State 1\n") lisp-out)
>       (setq state 1)
>       (setq i 0)
>       (cond
>        ((accept (FA s)) (princ (format "accepted\n") lisp-out))
>        (t (princ (format "rejected\n") lisp-out))
>       )
>     ) 
>   )
> )

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***