From: Pascal B. <pj...@in...> - 2021-05-07 03:14:22
|
Le 07/05/2021 à 04:44, Jean Louis a écrit : > * Kaz Kylheku <ka...@ky...> [2021-05-06 19:11]: >>> It is easy to find examples of Common Lisp as functional language, >>> rather hard to find procedural examples. >> >> Where are you looking? It's hard to find examples of Common Lisp as >> a procedural language in a book or tutorial that is ideologically >> geared toward functional. >> >> If you look in real code, it's not hard to find procedural coding. >> >> Just now, I headed to github and tried to think of some known CL project. >> >> For some reason, the word *cl-who* popped into my head, probably >> because my mind drifted toward authors and I was thinking "who" do >> I look for? >> >> And so, I searched for cl-who and immediately landed on this: >> >> https://github.com/edicl/cl-who/blob/master/who.lisp >> >> Whoa! Look, it's full of setf, loop, incf, nreverse ... > > IMHO I was expecting more style without `defun'. DEFUN is the operator to define functions. Programs are usually made of functions, and there's a "main" or "top-level" that is called when the program starts, which further calls other functions. That said, it's your lucky day, Common Lisp also deals with all other top-level forms. Usually, top-level forms are defining forms such as defun, but also defvar, defparameter, deftype, defmacro, defconstant, defclass, defgeneric, defmethod, define-condition, etc. Those defining forms let you instruct the compiler to create objects of various categories and name them. But Common Lisp also allows any other expression as top-level form. In that case, if the source file is compiled, the expressions are collected by the compiler in an initialization function that is automatically called at load time (when the compiled file is loaded), or, if the source file is loaded, they are evaluated at that time, one after the other. Therefore, it is possible to write a program, which will rather be called a "script" in that case, without using DEFUN. BUT, this is entirely orthogonal with whether your code is written in functional or procedural style, or any other style (declarative, OOP, whatever). For example, a file containing: #!/usr/local/bin/clisp -q -norc -ansi (let ((*print-right-margin* 16)) (pprint (mapcar (function string-upcase) (mapcar (function namestring) (directory "*.*"))))) named list-files, and with the execution access right (chmod +x list-files) would be a script written without DEFUN, in functional style. (apart, you may argue, PPRINT, but consider that's just an operator in the IO monad). [pjb@despina org.xquartz:0 tmp 36Gi]$ ~/list-files ("/PRIVATE/TMP/QUUX.LISP" "/PRIVATE/TMP/BAR.LISP" "/PRIVATE/TMP/FOO.LISP") [pjb@despina org.xquartz:0 tmp 36Gi]$ find * -prune -type f -ls 65058164 0 -rw-r--r-- 1 pjb wheel 0 May 7 05:12 bar.lisp 65058163 0 -rw-r--r-- 1 pjb wheel 0 May 7 05:12 foo.lisp 65034432 4 -rw------- 1 root wheel 36 May 7 04:43 fseventsd-uuid 65058165 0 -rw-r--r-- 1 pjb wheel 0 May 7 05:12 quux.lisp [pjb@despina org.xquartz:0 tmp 36Gi]$ -- __Pascal Bourguignon__ |