All commands, alphabetic order; letter N

This page contains the description of the following commands N2_seq, namestring, nconc, nconc1, nconc2, ncons, neg, negdistrib, negstep, newl, new_bep, nextl, nextprime, Nfactor, nth, nthcdr, not, NpowerM_seq, Npower_seq, nreconc, nreverse, null, numberp, number_of_divisors, numerator,


N2_seq() (Symbolic function)

The N2_seq() function produces the sequence of all pairs of non-negative integers. For details, see Npower_seq.

(1) N2_seq();

(1)                     <UninitializedSequence: N2>

(2) makeseq(%);

(2)                     <InitializedSequence: N2, 0>

(3) let(L=[], for x in 1...20 and y in N2_seq() do newl(L,y), reverse(L));

(3)  [[0, 0], [1, 0], [0, 1], [2, 0], [1, 1], [0, 2], [3, 0], [2, 1], 

[1, 2], [0, 3], [4, 0], [3, 1], [2, 2], [1, 3], [0, 4], [5, 0], [4, 1], 

[3, 2], [2, 3], [1, 4]]

(namestring x) (Lisp function)

The namestring function takes as argument a pathname (either an object of type pathname, or something that can be converted to a pathname, for instance a string or a symbol), like "foo/bar.gee" and returns the name as a string (here `foo/bar/gee'). See pathname for examples.

(nconc x1 ... xn) (Lisp function)

The nconc function takes a variable number of arguments. It concatenates them: non-list arguments are ignored, list arguments are chained, one after the other. If you say (nconc x x), then x is chained to itself, and some functions like length are unable to find the end of the list. You must be very careful with it. The function could be defined as follows

(defun nconc x
  (let ((res)) 
       (while (consp x) (setq res (nconc2 res (nextl x)))) 
       res))
(defun nconc2(x y)
  (if (atom x) y (rplacd (last x) y) x))
(defun nconc1(x y)
  (nconc2 x (ncons y)))

Examples

[Endymion] (setq l1 '(a b c) l2 '(d e f) l3 '(g h))
(g h)
[Endymion] (nconc l1 l2 l3)
(a b c d e f g h)
[Endymion] l1
(a b c d e f g h)
[Endymion] l2
(d e f g h)
[Endymion] l3
(g h)
[Endymion] (cirprint (list l1 l2 l3))
((a b c . #1=(d e f . #3=(g h))) #1# #3#)
((a b c d e f g h) (d e f g h) (g h))

(nconc2 x1 x2) (Lisp function)

The nconc2 function is the version with two arguments of nconc. The effect is to insert the second list at the end of the first.

[Endymion] (setq l1 '(1 2 3) l2 '(4 5 6))
(4 5 6)
[Endymion] (append l1 l2)
(1 2 3 4 5 6)
[Endymion] l1
(1 2 3)
[Endymion] (nconc2 l1 l2)
(1 2 3 4 5 6)
[Endymion] l1
(1 2 3 4 5 6)

(nconc1 x y) (Lisp function)

There is no difference between (nconc1 x y) and (nconc x (cons y ())). The effect is to add y at the end of the list x. It is like append1, but it modifies the first argument.

[Endymion] (setq l1 '(1 2 3))
(1 2 3)
[Endymion] (nconc1 l1 4)
(1 2 3 4)
[Endymion] (nconc1 l1 5)
(1 2 3 4 5)
[Endymion] (nconc1 l1 6)
(1 2 3 4 5 6)
[Endymion] (nconc1 l1 7)
(1 2 3 4 5 6 7)
[Endymion] (nconc1 2 3)
(3)
[Endymion] (nconc1 2 3 4)
nconc1 : wrong number of arguments : 3 this should be 2
[Endymion] (nconc1 '(1 . 2) 3)
(1 3)

(ncons a) (Lisp function)

The ncons function takes one argument. it creates a list of length one with this argument. There are alternate ways to obtain the same result.

[Endymion] (ncons 10)
(10)
[Endymion] (cons 10 ())
(10)
[Endymion] (list 10)
(10)
[Endymion] (xcons () 10)
(10)

neg(a) (Symbolic function)

The neg function takes one argument and computes the opposite of it. There is no difference between -x and neg(x). Negating an object is done by multiplying it by minus one in a context where negdistrib is true. The function could be defined as

neg(x):=
  if negdistrib then x*(-1)
  else let(negdistrib=true,x*(-1));

Examples

(1) neg(100);

(1)                                 - 100

(2) neg(1/2);

                                       1
(2)                                  - -
                                       2

(3) neg((a+b));

(3)                                - a - b

(5) let(negdistrib=true, (a+b)*(-1));

(5)                                - a - b

(6) let(negdistrib=false, (a+b)*(-1));

(6)                               - (a + b)

(7) neg("foo")?;
Bad type in arithmetic function: string for foo
Error : Cannot compute : neg(foo)

negdistrib (Symbolic variable)

The variable can be false, true or all (any value that is neither false nor all is replaced by true). If it is all then 2*(a+b) is transformed into 2*a+ 2*b (more generally: number times sum is expanded). If it is non-false then (-1)*(a+b) is transformed into -1*a + -1*b (more generally: minus one times sum is expanded). Note that -(a+b) is transformed into -a-b, because the sum is multiplied by minus one in a contexted where negdistrib is non false (see function neg above).

negstep (Keyword)

The construction 10...1 negstep 3 is explained here.

(newl a b) (Lisp macro)

newl(a, b) (Symbolic macro)

The newl macro takes two arguments. Its expansion is (setq a (cons b a)). In order for this to work, a should be a variable.

[Endymion] (setq x ())
()
[Endymion] (newl x 1) (newl x 2) (newl x 3)
(1)
(2 1)
(3 2 1)
[Endymion] (nextl x)
3
[Endymion] x
(2 1)
[Endymion] (nextl x y)
2
[Endymion] x
(1)
[Endymion] y
2
[Endymion] (setq T '(nextl x y))
(nextl x y)
[Endymion] (eval T)
1
[Endymion] T
(prog1 (setq y (car x)) (setq x (cdr x)))
[Endymion] (nextl)
nextl : wrong number of arguments : 0 this should be at least 1
[Endymion] (newl)
newl : wrong number of arguments : 0 this should be 2

The symbolic macro is defined by newl(x,y)::=buildq(x:=cons(y,x));. Normal usage is when x is a variable, whose value is a list. In the example below, we have shown the result of macro-expansion. Note that expression (1) is accepted by the compiler, the result is a highly recursive function. In case (8), you must remember that [a,b]:=[c,d] is the same as the parallel assignments a:=c and b:=d, the RHS is evaluated first, then need_length_equal is called, to check that this is a list with the right length. Thus using a list as first argument to newl does not work.

(1) newl(f(x),y);
Macro expansion -> newl(f(x), y)
Macro expansion <- f(x) := cons(y, f(x))
Macro expansion -> f(x) := cons(y, f(x))
Macro expansion <- f := nlambda(f, [x], cons(y, f(x)))

(1)                             <FUNCTION: f>

(3) L:=[1,2,4];

(3)                               [1, 2, 4]

(4) newl(L,5);
Macro expansion -> newl(L, 5)
Macro expansion <- L := cons(5, L)

(4)                              [5, 1, 2, 4]
(6) newl(17,0);
Error : not a variable : 17
(7) newl(w,0);
cons : not a list : w
(8) newl([a,b,c],d);
need_length_equal : length of list not 3 : [d, a, b, c]
(9) newl([a,dot,c],d);

(9)                              [a, dot, c]

Comments about (8) and (9) above: In fact, the compiler evaluates the equivalent of the Lisp expression (let (((A B C) '(1 2 3))) 'ok), replacing A, B, C by temporary variables, (1 2 3) by the RHS, 'ok by the assignment of the true variables to the temporaries. In the case of (let (((A . C) '(1 2 3))) 'ok), the quantity C is locally bound to (2 3) and (A . C) is called a dotted pair. In case (9), we have the symbolic equivalent of a dotted pair. The compiler evaluates something like (let (((A . C) '(d a . c))) 'ok). The result of it is that a is set to d and c to [a dot c]. The return value of the expression is that of the last assignment, namely the value of c. We show here the same example, showing all levels of macro-expansion.

(1) newl([a,b,c],d);
Macro expansion -> newl([a, b, c], d)
Macro expansion <- [a, b, c] := cons(d, [a, b, c])
Macro expansion -> [a, b, c] := cons(d, [a, b, c])
Macro expansion <- let([g3, g4, g5] = cons(d, [a, b, c]), a := g3, b := g4, c := g5)
Macro expansion -> let([g3, g4, g5] = cons(d, [a, b, c]), a := g3, b := g4, c := g5)
Macro expansion <- let([g6 = need_length_equal(cons(d, [a, b, c]), 3), 

g3 = 0, g4 = 0, g5 = 0], g3 := car(g6), g6 := cdr(g6), g4 := car(g6), 

g6 := cdr(g6), g5 := car(g6), a := g3, b := g4, c := g5)
need_length_equal : length of list not 3 : [d, a, b, c]
(4) newl([a,dot,c],d);
Macro expansion -> newl([a, dot, c], d)
Macro expansion <- [a, dot, c] := cons(d, [a, dot, c])
Macro expansion -> [a, dot, c] := cons(d, [a, dot, c])
Macro expansion <- let([g17, dot, g19] = cons(d, [a, dot, c]), a := g17, c := g19)
Macro expansion -> let([g17, dot, g19] = cons(d, [a, dot, c]), a := g17, c := g19)
Macro expansion <- let([g20 = need_length_greater(cons(d, [a, dot, c]), 0), 

g17 = 0, g19 = 0], g17 := car(g20), g20 := cdr(g20), g19 := g20, a := g17, c := g19)


(4)                              [a, dot, c]

NEXT() (Symbolic macro)

This macro jumps to a block named inner-do (see return for details about transfer of control). This macro is useful inside loops, since a block inner-do is created. See do for details. The code of the macro is given here.

NEXT()::=buildq(return("inner-do"));

(nextl a b) (Lisp macro)

The nextl macro takes one or two arguments. It returns the CAR of its first argument, and replaces it by its CDR. If two arguments are given, the CAR of the first argument is put in th CVAL of the second argument. This works only if the first argument is a variable, whose value is a list, and if two arguments are provided, that the second is a variable. The expansion is (prog1 (car a) (setq a (cdr a))) or (prog1 (setq b (car a)) (setq a (cdr a))) For an example, see nextl above.

[Endymion] (nextl 12)
car : not a list : 12
[Endymion] (nextl foo)
eval : undefined variable : foo
[Endymion] (setq foo 10)
10
[Endymion] (nextl foo)
car : not a list : 10
[Endymion] (setq foo '(10))
(10)
[Endymion] (nextl foo 12)
setq : not a variable : 12

(nextprime n) (Lisp function)

The nextprime function takes an argument n, and returns the smallest prime number greater than n. The prevprime function returns the greatest prime number smaller than its argument.

[Endymion] (mapcar 'nextprime '(1 2 3 5 7 9 11 13 17 19))
(2 3 5 7 11 11 13 17 19 23)
[Endymion] (mapcar (lambda (x) (nextprime (** 2 x))) '(1 2 3 4 5 6 7 8))
(3 5 11 17 37 67 131 257)
[Endymion] (mapcar (lambda (x) (nextprime (** 2 x))) '(10 20 30 40 50 60))
(1031 1048583 1073741827 1099511627791 1125899906842679 1152921504606847009)
[Endymion] (mapcar 'prevprime '(3 5 7 9 11 13 17 19))
(2 3 5 7 7 11 13 17)
[Endymion] (mapcar (lambda (x) (prevprime (** 2 x))) '(2 3 4 5 6 7 8))
(3 7 13 31 61 127 251)
[Endymion] (mapcar (lambda (x) (prevprime (** 2 x))) '(10 20 30 40 50 60))
(1021 1048573 1073741789 1099511627689 1125899906842597 1152921504606846883)

(Nfactor n) (Lisp function)

The Nfactor function checks the factorisation of a number. For details and examples, see ifactor.

(nth n x) (Lisp function)

nth(n, x) (Symbolic function)

The nth function takes two arguments, a non-negative integer and a list. It returns the n-th element of the list x. Assume that the list has N elements; an error is signaled in the symbolic case if n is not between 0 and N-1. The Lisp function returns the first element if n<;0, and nil if n is too big. The function could be defined as

(defun nth (x y)
   (if (neq (type-of x) 'fix) (nth errnia x))
   (while (and (> x 0) (consp y)) (setq x (- x 1) y (cdr y)))
   (if (consp y) (car y) ()))

Example (see under nthcdr for symbolic examples)

[Endymion] (nth 3 '(a b c d e f))
d
[Endymion] (nth 10 '(a b c d e f))
()
[Endymion] (nth -1 '(a b c d e f))
a
[Endymion] (nth 0 '(a b c d e f))
a
[Endymion] (nth 1 2 )
()
[Endymion] (nth 1/2 2 )
nth : not a fixnum : 1/2

(nthcdr n x) (Lisp function)

nthcdr(n, x) (Symbolic function)

The nthcdr function takes two arguments, a non-negative integer and a list. It returns the n-th CDR element of the list x. If the first argument is negative or zero, the whole list is returned. The function could be defined as

(defun nthcdr (x y)
   (if (neq (type-of x) 'fix) (nthcdr errnia x))
   (while (and (> x 0) (consp y)) (setq x (- x 1) y (cdr y)))
   (if (consp y) y ()))

Example

[Endymion] (nthcdr 1/2 2)
nthcdr : not a fixnum : 1/2
[Endymion] (nthcdr 3 '(a b c d e f))
(d e f)
[Endymion] (nthcdr 10 '(a b c d e f))
()
[Endymion] (nthcdr 0 '(a b c d e f))
(a b c d e f)
[Endymion] (nthcdr 3 '(a b c d . e))
(d . e)
[Endymion] (nthcdr 4 '(a b c d . e))
()

The symbolic function behaves like the Lisp function, but can signal a `list too short' error. It requires a positive value for n.

(1) nth(2/3, [1,2,3]);
#:feval:nth : not a fixnum : 2/3
(2) nthcdr(2/3, [1,2,3]);
#:feval:nthcdr : not a fixnum : 2/3
(3) nthcdr(23, [1,2,3]);
nthcdr : list too short : [1, 2, 3]
(4) nthcdr(2, [1,2,3,4]);

(4)                                 [3, 4]

(5) nth(2, [1,2,3,4]);

(5)                                   3

(6) nthcdr(5, x+y);
nthcdr : not a list : x + y

(not x) (Lisp function)

(null x) (Lisp function)

The not function takes one argument; it returns true if the argument is () and () otherwise. There is no difference between not and null.

[Endymion] (not ())
true
[Endymion] (not true)
()
[Endymion] (not 12)
()
[Endymion] (not 0)
()
[Endymion] (not 0.0)
()

NpowerM_seq(m) (symbolic function)

This function returns the m-th power of the integers, namely the sequence of all lists of length m of non-negative integers. See example below.

Npower_seq() (symbolic function)

This function returns power set of the integers, namely the sequence of all lists of any length m of non-negative integers. Let f be the function that maps the integers to the pair of integers. If f(n) is (a,b), we sort the numbers by increasing sum c=a+b, and for a fixed sum, by increasing b. The quantity c is asymptotically the square root of 2n, defined by the unique number such that d=n-c(c+1)/2 is between 0 and c. For instance, for n=19, we get c=5 and d=4. We have a=n-d and b=d. Let g(n,m) the function that returns the n-th list of length m. If m is one, the result is n, if m is two, the result is f(n). Otherwise, we compute f(n) as (a,b), then g(b,m-1), and insert a in front. Finally, the n-nth list is computed as follows: if n is zero, we return the empty list, otherwise, f(n+1) as (a,b), the result is then g(b,a+1).

(1) X:=NpowerM_seq(1);

(1)                     <UninitializedSequence: Nn, 1>

(2) Y:=NpowerM_seq(2);

(2)                     <UninitializedSequence: Nn, 2>

(3) Z:=NpowerM_seq(3)?;


(4) x:=makeseq(X);

(4)                    <InitializedSequence: Nn, 1, 0>

(5) y:=makeseq(Y);

(5)                    <InitializedSequence: Nn, 2, 0>

(6) z:=makeseq(Z)?;

(7) for t in 1...10 do print([popseq(x), popseq(y), popseq(z)]) ?;

[[0], [0, 0], [0, 0, 0]]
[[1], [1, 0], [1, 0, 0]]
[[2], [0, 1], [0, 1, 0]]
[[3], [2, 0], [2, 0, 0]]
[[4], [1, 1], [1, 1, 0]]
[[5], [0, 2], [0, 0, 1]]
[[6], [3, 0], [3, 0, 0]]
[[7], [2, 1], [2, 1, 0]]
[[8], [1, 2], [1, 0, 1]]
[[9], [0, 3], [0, 2, 0]]

(14)  A:=Npower_seq();

(14)                   <UninitializedSequence: Npower>

(15) a:=makeseq(A);

(15)                   <InitializedSequence: Npower, 0>

(16) for x in 1 ... 10 do print (popseq(a))?;
[]
[0]
[0, 0]
[1]
[0, 0, 0]
[1, 0]
[2]
[0, 0, 0, 0]
[1, 0, 0]
[0, 1]

(nreconc x y) (Lisp function)

The nreconc function takes two arguments x and y. It is equivalent to (nconc (nreverse x) y). If x is not a list, the second argument is returned. If it is a list, its value will be modified, as well as all pointers to it. The second argument is not modified.

[Endymion] (setq x '(1 2 3))
(1 2 3)
[Endymion] (setq y '(4 5 6))
(4 5 6)
[Endymion] (nreconc x y)
(3 2 1 4 5 6)
[Endymion] (setq l1 '(a b c d e))
(a b c d e)
[Endymion] (setq l2 (cdr l1))
(b c d e)
[Endymion] (setq l3 (last l1))
(e)
[Endymion] (nreconc l1 '(x y))
(e d c b a x y)
[Endymion] l1
(a x y)
[Endymion] l2
(b a x y)
[Endymion] l3
(e d c b a x y)
[Endymion] (nreconc '(1 . 2) '(3 . 4))
(1 3 . 4)

(nreverse x) (Lisp function)

The nreverse function takes one argument. It is equivalent to (nreconc x ()). The return value is the list in reverse order. The value of the list is modified (as well as all pointers to it). If the argument is not a list, nil is returned.

[Endymion] (setq l1 '(a b c d e))
(a b c d e)
[Endymion] (setq l2 (cdr l1))
(b c d e)
[Endymion] (setq l3 (last l1))
(e)
[Endymion] (nreverse l1)
(e d c b a)
[Endymion] l1
(a)
[Endymion] l2
(b a)
[Endymion] l3
(e d c b a)
[Endymion] (nreverse '(1 2 . 3))
(2 1)
[Endymion] (nreverse 12)
()

(numberp n) (Lisp function)

This function returns its argument if it is a number, and false otherwise. There are different type of numbers. Here a function that show the types.

(defun f(x)
   (catenate 
     (if (eq (type-of x) 'fix) "F"  " ")
     (if (eq (type-of x) 'float)"f"  " ")
     (if (integerp x) "I"  " ")
     (if (rationalp x) "R"  " ")
     (if (realp x) "r"  " ")
     (if (truecomplexp x) "C"  " ")
     (if (numberp x) "N"  " ")))
(defun g(x) (print (f x) " " x))
(mapc 'g (list 'x 0 12345678901 1/2 1.2 1.2L0 1.2B0  #C(1 2)))

Here is the result

        x
F IRr N 0
  IRr N 12345678901
   Rr N 1/2
 f  r N 1.2
    r N 0.12L1
    r N 0.120000000000000000000000000000000000000e1
     CN [2i+1]

(number_of_divisors n) (Lisp function)

The number_of_divisors function takes as argument a positive integer and returns the number of positive divisors. The function could be defined as follows

(defun number_of_divisors (n)
  (let ((res (ifactor n)) (r 1))
       (while (consp res) 
              (setq r (* r (+ 1 (cadar res))))
              (setq res (cdr res)))
       r))

Example

[Endymion] (number_of_divisors 1)
1
[Endymion] (number_of_divisors 2)
2
[Endymion] (number_of_divisors 10)
4
[Endymion] (number_of_divisors (** 2 10))
11
[Endymion] (number_of_divisors (fact 300))
9196638570974379572933091655680000000000

(numerator n) (Lisp function)

This function assumes taht the argument is an integer or a rational number. It returns the numerator

[Endymion] (numerator -2/3)
-2
[Endymion] (denominator -2/3)
3
[Endymion] (numerator -23)
-23
[Endymion] (denominator -23)
1
Endymion] (numerator -2.3)
numerator : not a rational : -2.3
[Endymion] (denominator -2.3)
denominator : not a rational : -2.3


Valid XHTML 1.0 Strict back to home page © INRIA 2005, 2006 Last modified $Date: 2009/01/08 17:43:30 $