Re: FP using CL
Jean Louis <[email protected]> Thu, 6 May 2021 09:28:44 +0300
| Newsgroups | gmane.lisp.clisp.general |
|---|---|
| Message-ID | <[email protected]> |
* Kaz Kylheku <[email protected]> [2021-05-03 22:41]: > On 2021-05-01 11:00, Duke Normandin wrote: > > Noob here! > > > > All of my previous non-pro, hobbyist hacking experience has been > > imperative. > > I want to give FP a shot using CL. > > Problem: I don't seem to grok how to start the process of creating a > > CL program using **just** functions. > > The good news is that pretty much "nobody" does that. Common Lisp is a > multi-paradigm language. It is easy to find examples of Common Lisp as functional language, rather hard to find procedural examples. When importing sets of data that require one time, single use programs, I may write it in imperative style. After that, program may be put in trash. Those are usually smaller programs that handle single file in a weird non-standard format. Not even one function has to be defined for such program to import the data. IMHO imperative or procedural programming is also great to start learning Common Lisp. > > I can learn the CL/Scheme/et al syntax, but it's the "Thinking in FP" > > that's killing me. :) Good book is: http://www.gigamonkeys.com/book/ If I remember well I had same process to figure it out, but then somewhere I read I should be making small functions, just enough to fit on screen that do simpler things. And I do that so. Rarely a function is larger than few lines. I do it, and I forget about it. Recommended reading: http://www.norvig.com/ To think in functional style, one should define functions that return some values, that may be used in other functions, higher functions, that end up in highest function calling the program. If your functions are small enough and made to be reusable, after building many functions you may find yourself writing less code for marvelous results. Let us say you wish to add, update, delete, copy entries in the address book. 1) Maybe you would first start with listing of all fields in the database, like first name, last name, city, country. That would be one function. (defun list-fields () (let ((list "GIVE ME ALL FIELDS FROM DATABASE")) list)) It returns the list. You would need to have function to display the list returned. 2) Other function would be maybe to edit value. Editing single value of any field. You would submit field to function and be able to edit it. Like (edit "Last name") could work to edit John Johnston's last name. As if function is generic, you will be able to edit any field with it. (defun edit-field (field) (let ((edited-value (edit-with-editor field))) (update-value database field value))) 3) Third one would be to update the value in the database. It would be sending the edited value into the database. Editing field would involve this function: (defun update-field (field value) (let ((database-command (format nil "PLEASE UPDATE FIELD ~A WITH VALUE ~A" field value))) (send-to-database database-command))) One can see tnnnhat `send-to-database' need to be another function. 4) Fourth could assign key bindings or menu. 5) Fifth one could list all entries on the screen. 6) Sixth could now call the listing of entries and call for key bindings. User would then be faced with the screen, would be able to press key, and by keys invoke all other functions. Jean Take action in Free Software Foundation campaigns: https://www.fsf.org/campaigns Sign an open letter in support of Richard M. Stallman https://stallmansupport.org/ https://rms-support-letter.github.io/ _______________________________________________ clisp-list mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/clisp-list