tutes-dump/site-tutorials/ecl_tutorial.html

247 lines
6.7 KiB
HTML
Raw Normal View History

2020-07-11 06:11:19 -04:00
<style type="text/css">
p {
margin-right:10em;
}
pre {
margin-left: 2em;
margin-right: 30em;
background-color: #ddd; padding: 10px;
color: green;
}
</style>
<h1>Using ECL (Embeddable Common-Lisp) on SDF</h1>
<h3>Contents:</h3>
<ul>
<li><a href="#what">What is ECL?</a></li>
<li><a href="#compare">ECL vs CLISP</a></li>
<li><a href="#basics">ECL Basics</a></li>
<li><a href="#oneliners">ECL one-liners</a></li>
<li><a href="#asdf">ASDF</a></li>
<li><a href="#customize">Customizations</a></li>
<li><a href="#refs">References</a></li>
</ul>
<h2 id="what">What is ECL?</h2>
<p>
ECL stands for <em>Embeddable Common-Lisp</em> and is a free open source
ANSI Common-Lisp implementation. Its distinguishing attributes are maintaining a
small-footprint, and being embeddable with existing C/C++ applications.
ECL is LGPL licensed and hosted at <a href="http://ecls.sourceforge.net/">ecls.sourceforge.net</a>.
</p>
<p><font color="brown">
ECL-11.1.1 is currently available to MetaARPA members on most SDF hosts.
</font></p>
<h2 id="compare">ECL vs CLISP</h2>
<p>
There are currently two Common-Lisp implementations available on SDF; ECL
and CLISP. Both are largely compliant with the ANSI Common-Lisp standard.
Each supports various object systems, debugging, bytecode compiling,
unicode, sockets, streams, etc. CLISP comes with built-in readline
support; ECL doesn't, though it can be added; see the <a href="#asdf">ASDF</a> section.
In addition to being smaller and faster than CLISP, ECL can be embedded
in C/C++ programs, allow embedded C/C++ code in Lisp programs, and produce
stand-alone ELF executables. ECL is also very well documented.
</p>
<h2 id="basics">ECL Basics</h2>
<p>
By default ECL starts up in interactive mode. Log into SDF host <em>miku</em> and
type <code>'ecl'</code>; you should see something like below:
</p>
<pre>
ECL (Embeddable Common-Lisp) 11.1.1
Copyright (C) 1984 Taiichi Yuasa and Masami Hagiya
Copyright (C) 1993 Giuseppe Attardi
Copyright (C) 2000 Juan J. Garcia-Ripoll
ECL is free software, and you are welcome to redistribute it
under certain conditions; see file 'Copyright' for details.
Type :h for Help.
Top level.
>
</pre>
<p>
The default ECL prompt is ">". Enter Common-Lisp commands in the usual way:
</p>
<pre>
> (+ 1 2 3)
6
> (* 4 5)
20
> (format t "hello SDF~&")
hello SDF
NIL
>
</pre>
<p>
In-line help is available ; type :h for options.
The debugger can be entered using <code>(break)</code> ; once entered typed :h to see options.
To exit the ECL interactive session type <code>:exit</code> .
</p>
<h2 id="oneliners">ECL one-liners</h2>
<p>
To use ECL as a script executor (ie. CGI) the <i>-shell</i> and/or <i>-eval</i> options
can be used. The <i>-norc</i> is often a good addition so as to avoid unnecessary
loading of modules, etc.
</p>
<p>ex: hello.lsp</p>
<pre>
% echo '(format t "~&Hello SDF~&")' > hello.lsp
% ecl -norc -script hello.lsp
Hello SDF
</pre>
<p>
It's also possible to use ECL to execute simple Common-Lisp one-liners at
the command line (note that <em>princ</em> is needed for screen output):
</p>
<pre>
% ecl -norc -shell /dev/null -eval "(princ (* 1 2 3))"
6
</pre>
<p>A nicer way to accomplish the same is to create a small shell script:</p>
<pre>
#!/bin/sh
# lispit - executes a lisp expression and dumps it to stdout
LSP_EXPR=${@}
if [ "$#" -eq 0 ]; then
echo "lispit - executes a lisp expression"
echo " usage: lispit '(lisp_expression)'"
echo ""
exit 1
fi
ecl -norc -shell /dev/null -eval "(princ ${LSP_EXPR} )"
echo ""
exit 0
</pre>
<p>
Example run:
</p>
<pre>
% lispit '(mapcar (lambda (x) (expt x 2)) `(1 2 3 4 5))'
(1 4 9 16 25)
</pre>
<h2 id="asdf">ASDF / enabling readline</h2>
<p>
ASDF (<i>Another System Definition Facility</i>) is an extensible build facility
for Common-Lisp software. ECL comes with ASDF bundled in, however on SDF,
users must setup their own ASDF repository under their $HOME directory.
</p>
<p>
The following outlines the process, using the <i>ecl-readline</i> module (adds
GNU readline support to ECL) as example:
</p>
<p>
1) Create the user-level ASDF repository:
</p>
<pre>
% mkdir -p $HOME/LISP/ASDF
</pre>
<p>
2) Create the ASDF config directory and config file*:
</p>
<pre>
% cd $HOME
% mkdir -p .config/common-lisp/source-registry.conf.d
% vi .config/common-lisp/source-registry.conf.d/asdf.conf
# ../asdf.conf
;;additional directory for ASDF to search (no recursion):
(:directory "<your_SDF_home_path>/LISP/ASDF/")
</pre>
<p>*note: you can call the conf file anything you want.</p>
<p>
3) Download ecl-readline module and copy select files to $HOME/LISP/ASDF:
</p>
<pre>
% cd /tmp
% snarf http://www.common-lisp.net/project/ecl-readline/releases/ecl-readline-0.4.1.tar.gz
% tar xzf ecl-readline-0.4.1.tar.gz
% cd ecl-readline-0.4.1/
% cp ecl-*.* $HOME/LISP/ASDF/
</pre>
<p>
4) Create the ECL startup file:
</p>
<pre>
# $HOME/.eclrc
(require 'asdf)
(asdf:operate 'asdf:load-op 'ecl-readline)
(ecl-readline::enable)
</pre>
<p>
The next time you startup ECL it will compile the ecl-readline module
and launch an interactive session. With ecl-readline enabled the default
ECL prompt is "<i>CL-USER[n]></i>" and you should then be able to use Emacs-style
command editing and history recall. If you don't like the provided prompt you
can change it by editing the <em>ecl-readline.lisp</em> file.
</p>
<h2 id="customize">Customizations</h2>
<p>
Additional modifications can be made to the ECL startup file, such as
user-defined functions and tweaks to various modules. Below is a more
nuanced example which reduces the verbosity at startup, modifies
ecl-readline's history file location, and adds an external SHELL function
similar to that found in CLISP:
</p>
<pre>
# $HOME/.eclrc
(require 'asdf)
(setf ASDF:*ASDF-VERBOSE* nil) ; quiets ASDF output some
(setf *load-verbose* nil) ; quiets the LOAD process some
(asdf:operate 'asdf:load-op 'ecl-readline)
(ecl-readline::enable :history-file "/var/tmp/.ecl-history")
;;
;; customizations
;; GNU clisp-like SHELL cmd
(defun shell (&optional (shell_cmd "$SHELL"))
"Args: (&optional shell_cmd)
SHELL calls the EXT:SYSTEM function. Executes SHELL_CMD if given, otherwise
User sub-shell is spawned. SHELL_CMD be string or symbol, 256 characters max."
(ext:system shell_cmd))
</pre>
<h2 id="refs">References:</h2>
<ul>
<li><em>ecl(1)</em> and <em>ecl-config(1)</em> manpages</li>
<li><a href="http://ecls.sourceforge.net/">http://ecls.sourceforge.net/</a></li>
<li><a href="http://common-lisp.net/project/asdf/">http://common-lisp.net/project/asdf</a></li>
<li><a href="http://common-lisp.net/project/ecl-readline/">http://common-lisp.net/project/ecl-readline</a></li>
<li><a href="http://www.cliki.net/Getting%20Started">Getting Started with Common-Lisp</a></li>
</ul>
<hr />
<cite>$Id: ecl_tutorial.html,v 1.4 2011/12/12 18:10:28 jgw Exp $</cite>