Author Topic: Nil  (Read 5541 times)

0 Members and 1 Guest are viewing this topic.

AWW

  • Guest
Nil
« on: October 28, 2011, 12:55:24 PM »
I know that Nil is the return value for an expression that is evaluated as false in the if/or/while functions. But what about coding similar to this:

(Function
      nil

Thanks for any input/clarification.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Nil
« Reply #1 on: October 28, 2011, 01:14:32 PM »
nil can also be an empty list:

nil == ()

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Nil
« Reply #2 on: October 28, 2011, 01:36:17 PM »
Nil always evaluates to an empty list in LISP

(null nil) -> T
(listp nil) -> T
(equal nil '()) ->T

Not like some languages where nil is NOT T

-David
R12 Dos - A2K

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Nil
« Reply #3 on: October 28, 2011, 02:24:34 PM »
More, nil is the only LISP data which is both considered as a list and as an atom:
(listp nil) => T
(atom nil) => T
Speaking English as a French Frog

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Nil
« Reply #4 on: October 28, 2011, 02:53:49 PM »
More, nil is the only LISP data which is both considered as a list and as an atom:
(listp nil) => T
(atom nil) => T


Amazing that the help files still do not list this:

atom
Verifies that an item is an atom
(atom item)
Arguments
item
Any AutoLISP element.
Some versions of LISP differ in their interpretation of atom, so be careful when converting from non-AutoLISP code.
Return Values
Nil if item is a list; otherwise T. Anything that is not a list is considered an atom.
Examples
Command: (setq a '(x y z))
(X Y Z)
Command: (setq b 'a)
A
Command: (atom 'a)
T
Command: (atom a)
nil
Command: (atom 'b)
T
Command: (atom b)
T
Command: (atom '(a b c))
nil

-David
R12 Dos - A2K

AWW

  • Guest
Re: Nil
« Reply #5 on: October 28, 2011, 03:10:48 PM »
So in this case, nil is a list or atom that returns T. Always thought nil was false and T was true.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Nil
« Reply #6 on: October 28, 2011, 03:44:56 PM »
T is true.
nil is both false and an empty list.

But you have to consider that LISP functions using predicates do not required the predicate to retun T (or nil), but a non nil value (or nil).
Speaking English as a French Frog

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Nil
« Reply #7 on: October 28, 2011, 04:23:06 PM »
nil
Do you consider nil equivalent to a empty list?
Or does a empty list just point to nothing(How can you have a empty list? By definition can it really be considered a list before items are added to it?)
 
I guess those questions can depend on how you look it.
Kinda like null for some languages.
Look at it is as the bits that represent 0 or
that it points to nothing.
 
 

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Nil
« Reply #8 on: October 28, 2011, 04:58:40 PM »
Yes,
nil can be seen as null (nothing) in some languages:  a variable wich hasn't been assigned is nil, set a variable to nil is the same as set a variable to null in some languages.

Quote
How can you have a empty list? By definition can it really be considered a list before items are added to it?
Yes,
you have to consider nil as an empty list too :
(cons 1 nil) returns (1)
as [] in F#:
1 :: [] returns [1]

And it is used as false in predicate too.
« Last Edit: October 28, 2011, 05:01:45 PM by gile »
Speaking English as a French Frog

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Nil
« Reply #9 on: October 28, 2011, 05:08:55 PM »
I knew AutoLisp would have to have a representaion for for a empty list and was asking more in a abstract sense,
but isn't Lisp just a bunch of linked list and nil represents the tail?

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Nil
« Reply #10 on: October 28, 2011, 05:20:54 PM »
Quote
but isn't Lisp just a bunch of linked list and nil represents the tail?
Yes, nil represents the tail of the last cons cell in a linked list.

(cons 1 (cons 2 (cons 3 (cons 4 nil)))) returns (1 2 3 4)


'(1 2 3 4) is a shortcut for: '(1 . (2 . (3 . (4 . nil))))

A French thread about cons cells and linked lists
« Last Edit: October 28, 2011, 05:25:42 PM by gile »
Speaking English as a French Frog

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Nil
« Reply #11 on: October 28, 2011, 06:04:24 PM »
I guess someone deleted a post but had this quoted
Quote
An AutoLISP variable that has not been assigned a value is said to be nil. This is different from blank, which is considered a character string, and different from 0, which is a number. So, in addition to checking a variable for its current value, you can test to determine if the variable has been assigned a value.   
     
Each variable consumes a small amount of memory, so it is good programming practice to reuse variable names or set  variables to nil when their values are no longer needed. Setting a variable to nil releases the memory used to store that variable's value. If you no longer need the val variable, you can release its value from memory with the following expression
_$ (setq val nil)

       
I guess the difference is or I does setting a variable to a empty list  also release the value?

hermanm

  • Guest
Re: Nil
« Reply #12 on: October 28, 2011, 06:35:18 PM »
T is true.


unless some bozo does this:

Code: [Select]
Command: (setq T nil)
nil

but:

Code: [Select]
Command: (eval t)
nil
Command: (atom t)
T

so,

Code: [Select]
Command: (setq t (atom t))
T

restores T to T

but if:
Code: [Select]
Command: (listp T)
T

we know that:
Code: [Select]
Command: (eval T)
nil

because nil is (as Gile pointed out) the only symbol which is both an atom & a list.

I know Gile knows all this - more for OP & other novices.:)

Ketxu

  • Newt
  • Posts: 109
Re: Nil
« Reply #13 on: October 28, 2011, 10:05:19 PM »
And Nil smaller all number ^^:
Quote
Command: (< nil -10000000)
T

Command: (< nil -10000000000000000000000000000)
T

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Nil
« Reply #14 on: October 29, 2011, 03:14:49 AM »
I guess someone deleted a post but had this quoted
Quote
An AutoLISP variable that has not been assigned a value is said to be nil. This is different from blank, which is considered a character string, and different from 0, which is a number. So, in addition to checking a variable for its current value, you can test to determine if the variable has been assigned a value.   
     
Each variable consumes a small amount of memory, so it is good programming practice to reuse variable names or set  variables to nil when their values are no longer needed. Setting a variable to nil releases the memory used to store that variable's value. If you no longer need the val variable, you can release its value from memory with the following expression
_$ (setq val nil)

       
I guess the difference is or I does setting a variable to a empty list  also release the value?

OK, I think I understand what you mean, and that makes sense.

Mainly, nil is nothing (null) because it's used to release memory, and it serves as well for some types explicitly defined in more strongly typed language (i.e. F#):
- the boolean false value
- an empty list (as [] in F#)
- the absence of a specific value as argument or function return (as () the F# unit type)

Here're some examples comparinf AutoLISP and F# (which is the strongly typed language closest to lISP I know).

nil as null (null isn't often used with F# which avoid the use mutable variables as it's more an imperative behavior)

Code: [Select]
_$ (setq s "foo")
"foo"
_$ (setq s nil)
nil

Code: [Select]
> let mutable s = "foo" ;;
val mutable s : string = "foo"

> s <- null ;;
val it : unit = ()


nil as false

Code: [Select]
_$ (= 0 1)
nil

Code: [Select]
> 0 = 1 ;;
val it : bool = false


nil as empty list

Code: [Select]
_$ (setq l nil)
nil
_$ (cons 1 l)
(1)

Code: [Select]
> let l = [] ;;

val l : 'a list

> 1 :: l ;;
val it : int list = [1]


nil as absence of a specific value

Code: [Select]
_$ (defun foo nil (alert "Hello"))
FOO
_$ (foo)
nil

Code: [Select]
> let foo () = printfn "Hello" ;;
val foo : unit -> unit

> foo () ;;
Hello
val it : unit = ()
« Last Edit: October 29, 2011, 05:14:10 AM by gile »
Speaking English as a French Frog