Nikie C
Nikie C

Reputation: 71

why i am said my indentation code is not correct?

I have to indent properly this code (common lisp):

Code 1

(defun supérieurs (nombre liste)
   (cond
      ((not liste) 0)
      ((>= (car liste) nombre) (+ 1 (supérieurs nombre (cdr liste))))
      ((supérieurs nombre (cdr liste))) ) )

Code 2

(defun supérieurs (nombre liste)
  (cond
      ((not liste) nil)
      ((>= (car liste) nombre) 
          (cons (car liste) (supérieurs nombre (cdr liste))) )
      ((supérieurs nombre (cdr liste))) ) )

Code 3

(defun nombres (liste)
   (cond
      ((not liste) 0)
      ((and (listp (car liste)) (numberp (car (car liste))))
            (+ 1 (nombres (cdr liste))) )   
      ((nombres (cdr liste))) ) )

Code 4

(defun nombre (liste)
  (cond
    ((not liste) nil)
    ((and (listp (car liste)) (numberp (car (car liste))))
        (cons (car liste) (nombre (cdr liste))) )   
    ((nombre (cdr liste))) ) )

Code 5

(defun begin (lettre liste)
  (cond
    ((atom liste) 0)
    ((and 
        (not (equal (car liste) nil)) 
        (equal (char (string (car liste)) 0) (char (string lettre) 0)))
        (+ 1 (begin lettre (cdr liste))) )
    ((begin lettre (cdr liste))) ) )

I don't understand why it's not.
Thanks for your help :)

Upvotes: 0

Views: 90

Answers (2)

Will Ness
Will Ness

Reputation: 71065

The purpose of indentation is to make the code readable, to be able to see the code's structure at a glance. More nested entities should be indented more than the outer level ones; same level -- the same. E.g. in (and ...xpr1... ...xpr2...), with non-trivial subexpressions, I much prefer the second on its separate line, indented under the first, as

    (and ...xpr1... 
         ...xpr2...)

Don't know if it's standard or not. I also prefer cond case's body indented at  2 spaces more than the test, even though the standard indentation is the same as the test -- which makes it an indiscernible wall of text, for me. YMMV.

Following these principles, I'd format your code snippets the following way (although as I mentioned, the cond formatting is non-standard and is bound to draw the ire of some).

Code 1

(defun supérieurs (nombre liste)
  (cond
    ((not liste) 0)
    ((>= (car liste) nombre)
       (+ 1 (supérieurs nombre (cdr liste))))
    ((supérieurs nombre (cdr liste)))))

Code 2

(defun supérieurs (nombre liste)
  (cond
    ((not liste) nil)
    ((>= (car liste) nombre) 
       (cons (car liste) 
             (supérieurs nombre (cdr liste))))
    ((supérieurs nombre (cdr liste)))))

Code 3

(defun nombres (liste)
  (cond
    ((not liste) 0)
    ((and (listp (car liste)) 
          (numberp (car (car liste))))
       (+ 1 (nombres (cdr liste))) )   
    ((nombres (cdr liste)))))

Code 4

(defun nombre (liste)
  (cond
    ((not liste) nil)
    ((and (listp (car liste))
          (numberp (car (car liste))))
       (cons (car liste) (nombre (cdr liste))))   
    ((nombre (cdr liste)))))

Code 5

(defun begin (lettre liste)
  (cond
    ((atom liste) 0)
    ((and (not (equal (car liste) nil)) 
          (equal (char (string (car liste)) 0)
                 (char (string lettre) 0)))
       (+ 1 (begin lettre (cdr liste))))
    ((begin lettre (cdr liste)))))

Upvotes: 0

Rainer Joswig
Rainer Joswig

Reputation: 139261

Use indentation of the editor. Avoid indenting with tabs.

(defun nombre (liste)
  (cond
    ((not liste) nil)
    ((and (listp (car liste)) (numberp (car (car liste))))
        (cons (car liste) (nombre (cdr liste))) )     
     ;  ^
     ;  |  Why this indentation?
    ((nombre (cdr liste))) ) )


(defun begin (lettre liste)
  (cond
    ((atom liste) 0)
    ((and 
        (not (equal (car liste) nil)) 
        (equal (char (string (car liste)) 0) (char (string lettre) 0)))
        (+ 1 (begin lettre (cdr liste))) )
     ;  ^
     ;  |  Why this indentation?
    ((begin lettre (cdr liste))) ) )


(defun nombres (liste)
   (cond
      ((not liste) 0)
      ((and (listp (car liste)) (numberp (car (car liste))))
            (+ 1 (nombres (cdr liste))) )
         ;  ^
         ;  |  Why this indentation?
   
      ((nombres (cdr liste))) ) )

Upvotes: 1

Related Questions