Orangutans grok Common Lisp
Please send bananas and DO NOT say "monkey"!
(cheers)
> 42 * 1:5 # The ':' operator produces a vector.
[1] 42 84 126 168 210
> a = array(11:14, dim = c(2, 2), dimnames = list(list("a", "b"), list("x", "y")))
> a
x y
a 11 13
b 12 14
In brief, you have a matrix with "named" rows and columns. There are other idiosyncrasies but this is already something interesting to deal with, as you can now write things like:> a[1, "y"] [1] 3 > a["b", ] x y 12 14The second "indexing" selects, or slices, the entire second row (counting from 1) and, as a result, returns a vector with the column names retained.
> a[2]
[1] 12
> a[42]
[1] NA # 'NA' is the 'not available' indicator.
> a[c(2, 3, 4, 10)]
[1] 12 13 14 NA
Indexing by only one index accesses the underlying vector in column major order. If the index is beyond the current limit, the value NA ('not available') is returned. If the sole index is a vector, then the indexing returns a vector of the values in the array accessed column major order.> a = array(11:19, dim = c(3, 3), dimnames = list(list("a", "b", "c"), list("x", "y", "z")))
> a
x y z
a 11 14 17
b 12 15 18
c 13 16 19
> a[2, "y"] = 42
> a
x y z
a 11 14 17
b 12 42 18
c 13 16 19
> b = array(42, dim = c(2, 2))
> b
[,1] [,2]
[1,] 42 42
[2,] 42 42
> a[2:3, 2:3] = b
> a
x y z
a 11 14 17
b 12 42 42
c 13 42 42
> a[2:3, 2:3] = c(1, 1, 1, 1) > a # This is not a surprise. x y z a 11 14 17 b 12 1 1 c 13 1 1 > a[2:3, 2:3] = c(1, 2, 3, 4, 5, 6) # This is also expected (modulo the 'multiple'.) Error in a[2:3, 2:3] = c(1, 2, 3, 4, 5, 6) : number of items to replace is not a multiple of replacement length > a[c(1, 2, 6, 7)] = c(111, 111, 111, 111) # Ok, understandable. > a x y z a 111 14 111 b 111 1 1 c 13 111 1 > a[c(1, 2, 12, 7)] = c(1, 1, 1, 1) > a # What?!? [1] 1 1 13 14 1 111 1 1 1 NA NA 1Ok, the last example shows how R manages assigning past the limit of the underlying vector.
make-array!)AT is introduced (and its counterpart, (SETF AT).)RHO) will look familiar to a R programmer, but it will not be the same. After all, is it Common Lisp, isn't it?AT Generic FunctionRHO 10 > (defvar qwerty "qwerty")
QWERTY
RHO 11 > (setf (at qwerty 3) #\R)
#\R
RHO 12 > qwerty
"qweRty"
RHO 13 > (at qwerty 4)
#\t
RHO 14 > (defvar a #2A((1 2 3) (4 5 6) (7 8 9)))
A
;;; ...
RHO 16 > (at a 1 1)
5
RHO 17 > (setf (at a 0 0) 42)
42
RHO 18 > a
#2A((42 2 3) (4 5 6) (7 8 9))
The generic function AT also works on structures and class instances.RHO 22 > (defstruct foo a s d)
FOO
RHO 23 > (defvar foo (make-foo :s 42))
FOO
RHO 24 > (at foo 'foo-s) ; In this case the accessor function FOO-S is invoked.
42
RHO 25 > (setf (at foo 'foo-d) "Food")
"Food"
RHO 26 > foo
#S(FOO :A NIL :S 42 :D "Food")
So far, so good. Now let's have some fun.RHO 28 > (setf rr (range 1 9)) ; This would be 'rr = 1:8' in R.
1 2 3 4 5 6 7 8
RHO 29 > (setf rv (vector 1 22/23 -3.2 42 5.8))
1 22/23 -3.2 42 5.8
RHO 30 > (setf rv2 (vector* (list 1 22/23 -3.2 42 5.8)
:names '(a b c d e)))
A B C D E
1 22/23 -3.2 42 5.8
The objects returned by VECTOR and VECTOR* (the full constructor) are wrappers around the underlying CL vectors. Their class is RHO:VECTOR. Note also that *PRINT-READABLY* is set to NIL, therefore, a "pretty printing" of the vectors is produced.RHO 37 > (setf ra (array (range 1 9) :dim '(3 3)))
[, 0] [, 1] [, 2]
[0, ] 1 2 3
[1, ] 4 5 6
[2, ] 7 8 1
RHO 38 > (at ra 1 1)
5
The function RANGE excludes the upper limit (following CL conventions), while the ARRAY constructors "recycles" the values passed to it (following R conventions). The value produces is pretty-printed, while its class is RHO:ARRAY. Indexing is 0-based, as in CL.RHO 3 > (setf rm (matrix '(1 1 2 0) :nrow 2 :ncol 2))
[, 0] [, 1]
[0, ] 1 1
[1, ] 2 0
RHO 4 > (at rm 0 1)
1
RHO 5 > (at rm 1 0)
2
RHORHO, modulo the Common Lisp background choices, bugs and unimplemented features; not many of the last kind.RHO 7 > (setf (dimension-names ra) '(("a" "b") ("x" "y")))
(("a" "b") ("x" "y"))
RHO 8 > ra
[, x] [, y] [, 2]
[a, ] 1 2 3
[b, ] 4 5 6
[2, ] 7 8 1
RHO 9 > (at ra 1 "y")
5
rb and use it as a value to set a sub-matrix of ra.RHO 13 > (setf rb (array 42 :dim '(2 2)))
[, 0] [, 1]
[0, ] 42 42
[1, ] 42 42
RHO 29 > (setf (at ra (vector 1 2) (vector 1 2)) rb)
#2A((42 42) (42 42))
RHO 15 > ra
[, x] [, y] [, 2]
[a, ] 1 2 3
[b, ] 4 42 42
[2, ] 7 42 42
As you can see, everything works as expected.rm; let's use it to set some elements of the array ra, according to the R "access-by-matrix" semantics.RHO 16 > rm [, 0] [, 1] [0, ] 1 1 [1, ] 2 0 RHO 24 > (type-of rm) MATRIX RHO 27 > (setf (at ra rm) 1024) 1024 RHO 28 > ra [, x] [, y] [, 2] [a, ] 1 2 3 [b, ] 4 1024 42 [2, ] 1024 42 42
RHO 29 > (setf (at ra #(1 2) #(1 2)) #(1 1 1 1)) #(1 1 1 1)
RHO 30 > ra
[, x] [, y] [, 2]
[a, ] 1 2 3
[b, ] 4 1 1
[2, ] 1024 1 1
RHO 42 > (setf (at ra #(1 2) #(1 2)) #(11 12 13 14 15))
#(11 12 13 14 15)
RHO 43 > ra
[, x] [, y] [, 2]
[a, ] 1 2 3
[b, ] 4 11 12
[2, ] 1024 13 14
Thanks to Masayuki Takagi, a not-so-obvious bug was fixed in the basic unification machinery of CL-UNIFICATION. This led to the addition of a couple of new utility functions and some other cleanups (hopefully).
The result should be in Quicklisp in the next round. Otherwise you can always clone/update/pull from the main repository.
I just revamped the CL-ENUMERATIONS library and API.
This was simply a nice cleanup of the previous library. The new documentation pages are, of course, generated with a little HEΛP, which also got some fine tuning to be able to generate doc pages from quite a variety of relatively "plain" doc-strings.
Hello there,
After a long time I finally had a few hours (sic!) to get back to actual hacking.
The main thing I did was to get myself re-accustomed with the new common-lisp.net infrastructure based on GitLab. This took some time to "realign" my repositories and getting my head wrapped around the way things work.
The result is that I went back to HEΛP to fix some tricky package issues, which are the result of wanting to be too cocky. Next I want to rework the web page layout in order to use the new HTML5 "semantic" tags.
After that, I will rework the documentation of my libraries and go on to "finish" a couple of other ones I have on the back burner.
Do I have too much on my lisping plate? :) :)
Cheers
enum. Common
Lisp can substitute the notion of enumerated type using the
member type specifiers.
(deftype colors () '(member red green blue))
public enum Operation { PLUS { double eval(double x, double y) { return x + y; } }, MINUS { double eval(double x, double y) { return x - y; } }, TIMES { double eval(double x, double y) { return x * y; } }, DIVIDE { double eval(double x, double y) { return x / y; } }; // Do arithmetic op represented by this constant abstract double eval(double x, double y); }
EQL specializers and using symbols as
enumerated type tags. enum numbers {ONE,
TWO,
FORTY_TWO = 42,
FORTY_THREE
};
defconstant's; alas, this breaks the use of
case and similar constructs.DEFENUM or
DEF-ENUM macro which provides functionalities
similar to C/C++ enumerated types, while adding a switch
or select macros as work-around the case
problem. A typical rendition of the C/C++ enum just
shown could be rendered as
(def-enum numbers ONE TWO (FORTY-TWO 42) FORTY-THREE)
DEFENUM macro is necessary.
DEFENUM macro
that merges most of the functionalities provided elsewhere. The
objective is, as always,
to make the simple things simple and the complex ones possible (and
possibly, simple as well).
(defenum season (spring summer fall winter))
deftype) with the four tags indicating the seasons. The
type checks work as expected, and the C/C++ behavior is also
reproduced, as the following shows:
cl-prompt> (typep 'fall 'season)
T
cl-prompt> (+ fall winter)
5
cl-prompt> (season-p 'spring) T cl-prompt> (season-p winter) ; No quote. T
season accesses the individual tags.
cl-prompt> (season winter)
WINTER
cl-prompt> (tags 'season)
(SPRING SUMMER FALL WINTER)
cl-prompt> (loop for season in (tags 'season) for s-id from spring do (format t "~S is followed by ~S~%" season (tag-of 'season (mod (1+ s-id) 4)))) SPRING is followed by SUMMER SUMMER is followed by FALL FALL is followed by WINTER WINTER is followed by SPRING NIL cl-prompt> (previous-enum-tag 'season 'fall) SUMMER
cl-prompt> (find-enum 'season)
#<ENUM SEASON (SPRING SUMMER FALL WINTER)>
season enumerated type is simple: nothing
particularly complicated. On the other hand, Java enumerated types
show that is possible to extend the notion of enumerated type in an
interesting way, by introducing the notion of structured
enumerated type.
Planet and the Operation enumerated
types.
public enum Planet { MERCURY (3.303e+23, 2.4397e6), VENUS (4.869e+24, 6.0518e6), EARTH (5.976e+24, 6.37814e6), MARS (6.421e+23, 3.3972e6), JUPITER (1.9e+27, 7.1492e7), SATURN (5.688e+26, 6.0268e7), URANUS (8.686e+25, 2.5559e7), NEPTUNE (1.024e+26, 2.4746e7), PLUTO (1.27e+22, 1.137e6); private final double mass; // in kilograms private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
public double mass() { return mass; }
public double radius() { return radius; }
// universal gravitational constant (m3 kg-1 s-2)
public static final double G = 6.67300E-11;
public double surfaceGravity() {
return G * mass / (radius * radius);
}
public double surfaceWeight(double otherMass) {
return otherMass * surfaceGravity();
}
}
Planet enumerated type is
the following (the units are unimportant):
$ cat Planet.java
public enum Planet {
...
public static void main(String[] args) {
double earthWeight = Double.parseDouble(args[0]);
double mass = earthWeight/EARTH.surfaceGravity();
for (Planet p : Planet.values())
System.out.printf("Your weight on %s is %f%n",
p, p.surfaceWeight(mass));
}
}
$ java Planet 175
Your weight on MERCURY is 66.107583
Your weight on VENUS is 158.374842
Your weight on EARTH is 175.000000
Your weight on MARS is 66.279007
Your weight on JUPITER is 442.847567
Your weight on SATURN is 186.552719
Your weight on URANUS is 158.397260
Your weight on NEPTUNE is 199.207413
Your weight on PLUTO is 11.703031
Planet enumerated type and the
test program above are rendered in Common Lisp as
follows: (defconstant g 6.67300D-11)
(defenum (planet (:initargs (mass radius)))
((MERCURY (3.303D+23 2.4397D6))
(VENUS (4.869D+24 6.0518D6))
(EARTH (5.976D+24 6.37814D6))
(MARS (6.421D+23 3.3972D6))
(JUPITER (1.9D+27 7.1492D7))
(SATURN (5.688D+26 6.0268D7))
(URANUS (8.686D+25 2.5559D7))
(NEPTUNE (1.024D+26 2.4746D7))
(PLUTO (1.27D+22 1.137D6))
)
((mass 0.0d0 :type double-float)
(radius 0.0d0 :type double-float)
)
(:documentation "The Planet Enum.")
(:method surface-gravity ((p planet))
(* g (/ mass (* radius radius))))
(:method surface-weight ((p planet) other-mass)
(* other-mass (surface-gravity p)))
)
cl-prompt> (let* ((earth-weight 175)
(mass (/ earth-weight (surface-gravity 'earth)))
)
(dolist (p (tags 'planet))
(format t "Your weight on ~S is ~F~%"
(tag-name p)
(surface-weight p mass)))
Your weight on MERCURY is 66.10758266016366
Your weight on VENUS is 158.37484247218296
Your weight on EARTH is 174.99999999999997
Your weight on MARS is 66.27900720649754
Your weight on JUPITER is 442.84756696175464
Your weight on SATURN is 186.55271929202414
Your weight on URANUS is 158.39725989314937
Your weight on NEPTUNE is 199.20741268219015
Your weight on PLUTO is 11.703030772485283
tag-name is necessary because the
print-object method for structured tags is not
too extreme; it could be made to print the tag name.cl-prompt> (planet-p 'venus) T cl-prompt> (planet-p 'vulcan) NIL
Operation example shows how to define methods
that are actually specialized on tags (the rationale is to avoid
writing error-prone non-object-oriented switch
statements. The Java Operation
is the following:
public enum Operation { PLUS { double eval(double x, double y) { return x + y; } }, MINUS { double eval(double x, double y) { return x - y; } }, TIMES { double eval(double x, double y) { return x * y; } }, DIVIDE { double eval(double x, double y) { return x / y; } }; // Do arithmetic op represented by this constant abstract double eval(double x, double y); public static void main(String args[]) { double x = Double.parseDouble(args[0]); double y = Double.parseDouble(args[1]); for (Operation op : Operation.values()) System.out.printf("%f %s %f = %f%n", x, op, y, op.eval(x, y)); } }
EQL specialized methods, and
DEFENUM directly provides for this idiom. Actually
there are two equivalent forms for it.
(defenum operation (PLUS MINUS TIMES DIVIDE) () (:method evaluate ('plus x y) (+ x y)) ; Note the 'quote' shorthand. (:method evaluate ('minus x y) (- x y)) (:method evaluate ('times x y) (* x y)) (:method evaluate ('divide x y) (/ x y)) )
(defenum operation ((PLUS () (:method evaluate (x y) (+ x y))) ; Note the 'no tag argument' shorthand. (MINUS () (:method evaluate (x y) (- x y))) (TIMES () (:method evaluate (x y) (* x y))) (DIVIDE () (:method evaluate (x y) (/ x y))) ))
cl-prompt> (let ((x 4) (y 2))
(dolist (op (tags 'operation))
(format t "~S ~S ~S = ~S~%" x (tag-name op) y (evaluate op x y))))
4 PLUS 2 = 6
4 MINUS 2 = 2
4 TIMES 2 = 8
4 DIVIDE 2 = 2
NIL
// rgb.cc -- #include <iostream> using namespace std; enum rgb { RED = 0xff0000, GREEN = 0x00ff00, BLUE = 0x0000ff }; int mix_colors(enum rgb c1, int c2, int c3) { return c1 | c2 | c3; } int main() { enum rgb basic_color = GREEN; cout << "The basic color chosen is: " << basic_color << endl; cout << "WHITE is: " << (RED | GREEN | BLUE) << endl; cout << "WHITE is also: " << mix_colors(RED, GREEN, BLUE) << endl; } // end of file -- rgb.cc --
cl-prompt> (defenum (colors (:initargs (r g b))) ((red #xff0000 (255 0 0) (:method mix (c2 c3) (logior red c1 c2))) (green #x00ff00 (0 255 0) (:method mix (c2 c3) (logior green c1 c2))) (blue #x0000ff (0 0 255) (:method mix (c2 c3) (logior blue c1 c2))) ) ((r 0 :type (integer 0 255)) (g 0 :type (integer 0 255)) (b 0 :type (integer 0 255)) ) (:documentation "The Colors Enum.")) #<ENUM COLORS (RED GREEN BLUE)> cl-prompt> (format t "The color WHITE is ~D~%" (mix red green blue)) ; Yes, this works as is! The color WHITE is 16777215 cl-prompt> (mapcar #'colors-r (tags 'colors)) (255 0 0)