Showing posts with label Common Lisp. Show all posts
Showing posts with label Common Lisp. Show all posts

Sunday, February 24, 2008

Common lisp IDE: CUSP for Eclipse.


Cusp
is Eclipse plugin for Lisp Development.

Below is description of its installation and starting.

Procedure and pictures given on (k)Ubuntu Gutsy 7.10 and KDE.

Should work on Debian too.

Install eclipse:


$ sudo apt-get install eclipse

Start eclipse :

KDE Menu->Development->Eclipse.

From eclipse:

Help->Software Updates->Find and Install.
Fill definition of update site for CUSP.

Mark CUSP site for update; Finish
Accept licenses:
Install All:
Restart;
Then Eclipse->New Project-> Lisp.
Watch Lisp IDE How it looks.
Update main module; Add yours:
Evaluate it as written in comment:
Explore Lisp menu for further functions.

Monday, February 18, 2008

Common Lisp Hyperspec Statistics

I have got tar.gz HyperSpec of common lisp. As stated
"The Common Lisp HyperSpec consumes just over 15MB of disk storage in about 2300 files. It contains approximately 105,000 hyperlinks!"

I am more interested in other statistics, that is statistics of what articles are most linked. I have to check this articles first; they seem to be basis for all other staff.

Link to received statistics file is at the end of the post.

Following is description of way of obtaining this statistics.

So first, gunzip and untar downloaded HyperSpec.tar.gz,
go to dir untared.

Check number of files, is there such amount as reported.


../HyperSpec$ ls
Body Data Front Graphics Issues
../Hyperspec$ lynx Body/[TAB]
Display all 1502 possibilities? (y or n)(n)
$ find ./ -type f | wc
2342 2342 45568
$

Than, prepare full document body in plain text:

$ find ./ -type f | xargs lynx --dump > temp.txt

Check number of links:

$ cat temp.txt | grep "file:" | wc
130033 260085 12115448


prepare csv with statistics:


$ cat temp.txt | grep "file:" |
perl -e 'while (<>)
{ @a=split(/file\:/);
$hash{$a[1]}++;} ;
for my $key (keys %hash)
{ print "$hash{$key};$key"; }
' > temp.csv


Now get top 20 linked articles:
$ cat temp.csv | sort -n | tail -20
850;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Body/26_glo_s.htm#symbol
1035;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Body/26_glo_o.htm#object
1156;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Body/26_glo_t.htm#type
1240;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Body/a_nil.htm#nil
1360;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Body/t
1372;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Body/s
2122;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Body/m
2190;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Front/StartPts.htm
2190;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Front/X_Master.htm
2190;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Front/X_Symbol.htm
2227;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Front/Help.htm#Disclaimer
2245;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Body/26_a.htm
2248;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Front/Contents.htm
2865;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Body/a
3216;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Front/X3J13Iss.htm
4417;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Front/Help.htm#Legal
4530;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Front/index.htm
6455;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Body/f
7121;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Issues
7679;//localhost/home/rtg/stored/programming/lisp%26emacs/HyperSpec/Front/


Complete .csv with statistics placed there.

Now I have to place this file to my PDA near the Hyperspec, to use it as plan for reading in metro . TG, Pocket Excel recognizes .csv;)

Sunday, February 17, 2008

Perl nested lists vs. Python and Lisp.

It is no nested lists in Perl. However, Python and Lisp have this ability.

Description of implementing in Perl equivalent structure to nested Python and Lisp lists given.

Used:

$ python --version
Python 2.5.1
$ perl -v | head -2

This is perl, v5.8.8 built for i486-linux-gnu-thread-multi
$ clisp --version | head -1
GNU CLISP 2.41 (2006-10-13) (built 3371977993) (memory 3401903509)


Consider
'(1 (2 (3) 4) 5)
lisp list as the example of data structure we work with:

$ clisp
...
[1]> (setq alist `(1 (2 (3) 4) 5))
(1 (2 (3) 4) 5)
[2]> (elt alist 0)
1
[3]> (elt alist 2)
5
[4]> (elt alist 1)
(2 (3) 4)
[5]>

Corresponding interactive session for Python:

>>> a = [1, [2, [3], 4], 5]
>>> print a
[1, [2, [3], 4], 5]
>>> print a[0]
1
>>> print a[1]
[2, [3], 4]
>>> print a[2]
5



Python in list processing is very like Lisp. The difference for defining list body is only using commas and square brackets in python, and you don't need put quote sign before it. (As done in Lisp to interpret it as list, not executing as function).

Perl is different in this point. Perl instead of interpreting list as given, removes all parens inside.


$ perl -e '@al = (1, (2, (3), 4), 5); \
print "\@al[0]=@al[0] , \@al[1]=@al[1] , \@al[2]=@al[2] ,\
\@al[3]=@al[3] , \@al[4]=@al[4] \n"; \
print @al; print "\n"; '
@al[0]=1 , @al[1]=2 , @al[2]=3 , @al[3]=4 , @al[4]=5
12345


Lisp has very convenient data model, all data in lisp are pointers.
And the solution of implementing nested lists in Perl is pointer based.

Pointer to list in perl uses square brackets ([]), like lists syntax in Python.
So our structure look is:

$al = [1, [2, [3], 4], 5];


Following procedure prints elements, emulating output in Python session and Common Lisp REPL:


sub slistp($)
{
my ($listp) = @_;
my $i;
my $ret = "";
if (ref($listp) eq 'ARRAY')
{
$ret = "[";
$ret .= slistp(@$listp[0]);
for ($i=1;$i<(scalar @$listp);$i++)
{
$ret .= ",";
$ret .= slistp(@$listp[$i]);
};
$ret .= "]";
}
else
{
$ret .= $listp;
}
return($ret);
}


Usage for our list:
$al = [1, [2, [3], 4], 5];
print "\@al[0]=".@$al[0].", \@al[1]=@$al[1] , \@al[2]=@$al[2] \n";
print slistp($al);
print "\n";
print "\@al[0]=".slistp(@$al[0])."\n\@al[1]=".slistp(@$al[1]);
print "\n\@al[2]=".slistp(@$al[2])."\n";

result of complete code execution:
$ perl nestlist.pl
@al[0]=1, @al[1]=ARRAY(0x8152b44) , @al[2]=5
[1,[2,[3],4],5]
@al[0]=1
@al[1]=[2,[3],4]
@al[2]=5


So for Perl lisp-based tree structures, list references have to be used instead of lists.

Additional fees for dereferencing array pointers applied in Perl syntax.
It is no REPL integrated session in Perl, and examining complex structures based on lists require usage of additional modules, or writing own functions. Therefore Python, and especially Lisp are more preferable for list processing tasks.

Tuesday, February 12, 2008

Common lisp: Lazy function definition and redefinition

Following example demonstrates ability to build function from the list in common lisp , execute this list to obtain definition of function; redefinition of function from the other list, executing redefinition for instantiation of the function.

SBCL was used.


-----------%<-------------

;; Copyright (c) Roman T. Gritsulyak, 2008,
;; echo "(append '(roman.gritsulyak) '(\@) '(gmail.com))" | clisp -q

Break 29 [33]> (defvar *my_fun*)
*MY_FUN*
Break 29 [33]> (setf *my_fun* (append '(defun) '(my(x)) '((* x x))))
(DEFUN MY (X) (* X X))
Break 29 [33]> (eval *my_fun*)
MY
Break 29 [33]> (my 5)
25
Break 29 [33]> (setf *my_fun* (append '(defun) '(my(x)) '((+ x x))))
(DEFUN MY (X) (+ X X))
Break 29 [33]> (my 5)
25
Break 29 [33]> (eval *my_fun*)
MY
Break 29 [33]> (my 5)
10
----------->%-------------

Friday, February 8, 2008

Common Lisp vs Python: code size

Just joke :)

I know, it is not enough statistics for conclusions :)

We have compared reduce statement in Common lisp and python with one fellow.
I wrote in Common lisp, he in Python.

Resulting "code" sizes for same construct, just translated from one language to another:


$ echo "reduce(lambda x,y: (x,y,x), (1,2,3,4))" | wc
1 4 39
$ echo "(reduce (lambda(x y)(list x y x)) (list 1 2 3 4))" | wc
1 11 50
Python : 39 symbols long
Common Lisp : 50 symbols.


And the winner is Python :)
Python win about 20 or 25 percents of code size.