list and string

Discussion of Common Lisp

list and string

Postby phdenis40 » Fri May 20, 2016 4:41 am

Hello all,

I thought naively that the following code will allow me to print each element of a list and not display the nil element at the end.
Is it normal ?
Code: Select all
(setq mylst '(chat chien oiseau lapin))

(loop for n from 0 to (- (length mylst) 1)
 do (print (nth n mylst)))


Do I have to add a condition to check that the nth n mylst is not nill like
Code: Select all
(null (nth n mylst))


My main idea is to convert each element as a sub-string like "chat chien oiseau lapin".

Thanks by advance for your help.
phdenis40
 
Posts: 15
Joined: Sun Mar 06, 2016 5:00 pm

Re: list and string

Postby Goheeca » Fri May 20, 2016 8:24 am

That nil is the value of the loop expression. And better way to print list this was is via format function.
Code: Select all
(format t "~{~a~^ ~}" mylst)
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.
User avatar
Goheeca
 
Posts: 271
Joined: Thu May 10, 2012 12:54 pm

Re: list and string

Postby pjstirling » Fri May 20, 2016 10:06 am

Or

Code: Select all
(dolist (el mylst)
  (print el))


Friends don't let friends do LOOP!
pjstirling
 
Posts: 140
Joined: Sun Nov 28, 2010 4:21 pm

Re: list and string

Postby phdenis40 » Fri May 20, 2016 10:43 am

Thanks for the answer but both solution displayed a NIL line.
I don't want the NIL line
Code: Select all
Break 4 [6]> (setq mylst '(chat chien oiseau lapin))
(CHAT CHIEN OISEAU LAPIN)
Break 4 [6]> (dolist (el mylst)
(print el))

CHAT
CHIEN
OISEAU
LAPIN
NIL
Break 4 [6]> (format t "~{~a~^ ~}" mylst)
CHAT CHIEN OISEAU LAPIN
NIL
Break 4 [6]> (format t "~{~a~^ ~}" mylst)
CHAT CHIEN OISEAU LAPIN
NIL
phdenis40
 
Posts: 15
Joined: Sun Mar 06, 2016 5:00 pm

Re: list and string

Postby Goheeca » Fri May 20, 2016 11:15 am

As I said the NIL is an evaluated value of the expression you've entered into REPL (the interactive mode you are woking with). That's how the REPL works:
  • prompts you with a prompt line.
  • you enter an expression.
  • prints whatever you write into the output stream (through print/write functions).
  • prints a value of your expression
  • and loops

If you write your program to a script file and run the interpreter in a script mode (e.g. for the SBCL implementation you have a --script switch at disposal) then you only see what you printed (without your NIL values).
If you are using interactive mode you have some options.
  • Use more modular approach and let the functions return produced values instead of printing them, but the strings will be quoted by #\" characters (as it's standard way of doing it).
  • The another possibility is use values like this:
    Code: Select all
    (progn (... your code ...) (values))

    The code does the thing that it doesn't return any value, but (I think) it's up to an implementation how it will be represented in REPL. Yeah in SBCL it really is no output, but it doesn't have to be everywhere. Moreover this piece of code is only bloating your code.
I recommend don't be bothered by the NILs at the end, because:
  • you, as a programmer using REPL, know what that NIL means.
  • you, as user, will be using program where the NILs won't be present.

Finally take a look this SLIME session:
Image

Printed content and values of expressions are clearly distinguished by color, further, as you can see, no value in SLIME is indicated by the line
Code: Select all
; no value

and not just nothing.
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.
User avatar
Goheeca
 
Posts: 271
Joined: Thu May 10, 2012 12:54 pm

Re: list and string

Postby phdenis40 » Mon May 30, 2016 4:40 am

Hello,

In my code, I've a stack overflow problem. Thus, I'm wondering if it exists some debuggers for Windows or Linux?
I've a computer with cygwin under Windows or Ubuntu OS.

Thanks by advance for your help.
phdenis40
 
Posts: 15
Joined: Sun Mar 06, 2016 5:00 pm

Re: list and string

Postby pjstirling » Mon May 30, 2016 12:21 pm

All common-lisp implementations have a debugger built-in, which implementation are you using?
pjstirling
 
Posts: 140
Joined: Sun Nov 28, 2010 4:21 pm

Re: list and string

Postby phdenis40 » Mon May 30, 2016 2:18 pm

clisp on cygwin
phdenis40
 
Posts: 15
Joined: Sun Mar 06, 2016 5:00 pm

Re: list and string

Postby pjstirling » Mon May 30, 2016 4:08 pm

Hmm, I see what you mean:

Code: Select all
[3]> (defun tato () (tato))

TATO
[4]> (tato)

*** - Program stack overflow. RESET


Clisp does have a debugger, but for program stack overflow it doesn't use it, and just resets.

Infinite recursion is a sticky case, in general, for programs to recover from.

The stack...
  1. contains all of the local variables for each function in the current thread of execution.
  2. is allocated when a thread is created, with a particular size,
  3. cannot be re-sized at run-time, because heap memory might have been allocated next to the stack,
  4. cannot be re-located either, because you would need to re-write any pointers to stack locations (which is impossible to do correctly).

The result of this is that when you run out of stack you can't invoke any functions that need local variables (because that would push you even further into danger territory). Some systems keep a chunk of memory aside (in the heap) to store the temporary variables it might need in the event of such a problem, but clisp, apparently, doesn't do that.

Depending on your code you may be able to track the infinite recursion by using TRACE on your suspect function, alternatively you can use one of the clisp methods for starting with an increased stack size (if you don't have infinite recursion, just very deep function calls).

http://www.clisp.org/impnotes.html#faq-stack
pjstirling
 
Posts: 140
Joined: Sun Nov 28, 2010 4:21 pm

Re: list and string

Postby phdenis40 » Tue May 31, 2016 11:54 pm

Hello,
I sent you a private message linked to this stack overflow problem.

Code: Select all
(defun MyComp (MyInp MyList)
  (cond
    ((and (not MyInp) (not MyList)))
    ((not MyList) nil)
    ((eq (car MyList) '*)
      (or
        (MyComp MyInp (cdr MyList))
        (MyComp (cdr MyInp) MyList)
     )
    )
   
    ((not MyInp) nil)
    ((eq (char (string (car MyInp)) 0) '#\?)
      (and
        (> (length (car MyInp)) 1)
        (put 'Variables (MyFirst (car MyList)) (car (MyInp)))
      )
      (MyComp (cdr MyInp) (cdr MyList)) )

    ((eql (car MyInp) (car MyList)) (MyComp (cdr MyInp) (cdr MyList)))
  )
)

;; Normally, its the first atom without the 1st caracter
(defun MyFirst (MySubList)
  (print MySubList)
)


with MyFirst is a function which returns the 1st element without the 1st caracter.
phdenis40
 
Posts: 15
Joined: Sun Mar 06, 2016 5:00 pm

Next

Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 4 guests

cron