Functional Programming Originates from the Urgent Need of Weak Mankind to Resist the Great Universe(en)

Take the Common Lisp program as an example.

A Common Lisp program could have been just a function, but in order to make it easier for humans to read it, a function was split into multiple small functions. As for modular reusability, it is easy to debug, and it is an unexpected result. If the entire program is written in only one function, because the screen for displaying the program is limited, the naked eye's perspective is limited, the content captured by the naked eye at one time performance will be very limited, then each time the mobile screen and vision are acquired The information will be greatly reduced. The dual limitations of the physical screen and the physiological perspective, oppressing humans to manually split the function process, but its fundamental, in fact, "one of the best in the sea, send in the world" helplessness. The little ones, however, want to observe the vastness of the sky. Maybe the whole world is the human race itself, that is, all beings, and “one flower and one world”.

The function "string-to-html" is intended to convert large sections of text into text that conforms to the "HTML" syntax. If it is a section of text, the "<p></p>" tag is added. If it is a blank line, Just add the "<p><br/></p>" tag.

For example, given a large section of text:

(defparameter *string-to-html*
  "0

1")

Need to be converted into:

"

0


1

"

When not splitting a function, all operations are contained in a function:

(defun string-to-html (string)
  (format nil "~{

~A

~}"
(mapcar (lambda (i) (if (or (null i) (and (stringp i) (= (length i) 0))) "
"
i)) (let ((var nil) (stream (make-string-input-stream string))) (do ((line (read-line stream nil 'eof) (read-line stream nil 'eof))) ((eql line 'eof)) (push line var)) (reverse var)))))

transfer:

(string-to-html *string-to-html*)

"

0


1

"

Split global function:

(defun null-string-p (string)
  (or (null string)
      (and (stringp string)
           (= (length string)
              0))))

(defun %%string-to-html-1 (string)
  (let ((var nil)
        (stream (make-string-input-stream string)))
    (do ((line (read-line stream nil 'eof)
               (read-line stream nil 'eof)))
        ((eql line 'eof))
      (push line var))
    (reverse var)))

(defun %string-to-html-1 (%%string-to-html-1)
  (mapcar (lambda (i)
            (if (null-string-p i)
                "
"
i)) %%string-to-html-1)) (defun string-to-html-1 (%string-to-html-1) (format nil "~{

~A

~}"
%string-to-html-1))

When invoked:

(string-to-html-1
 (%string-to-html-1
  (%%string-to-html-1 *string-to-html*)))

"

0


1

"

Split after the local function:

(defun string-to-html-2 (string)
  (labels ((null-string-p (string)
             (or (null string)
                 (and (stringp string)
                      (= (length string)
                         0))))
           (%%string-to-html-2 (string)
             (let ((var nil)
                   (stream (make-string-input-stream string)))
               (do ((line (read-line stream nil 'eof)
                          (read-line stream nil 'eof)))
                   ((eql line 'eof))
                 (push line var))
               (reverse var)))
           (%string-to-html-2 (%%string-to-html-2)
             (mapcar (lambda (i)
                       (if (null-string-p i)
                           "
"
i)) %%string-to-html-2))) (format nil "~{

~A

~}"
(%string-to-html-2 (%%string-to-html-2 string)))))

When comparing the above three implementations, when splitting into a global function:

  • Cost
    • Need to input more characters and number of lines, the physical cost: (90-54)/54=2/3
      • Character without splitting function: Region has 16 lines, 54 words, and 661
        Characters.
      • Partitioned local function: Region has 23 lines, 90 words, and 899
        Characters.
      • Split global function: Region has 25 lines, 90 words, and 673
        Characters.
    • Need to choose a suitable name for multiple small functions, the spiritual cost: abstract a process with a name
      • Need to combine functions
      • Need to see the function name when debugging
  • Benefits
    • Easy to read: small content, indentation, obvious function functions.
    • Modularization: When using the return value, it is only necessary to ensure that the parameters meet the requirements and naturally return the appropriate values.
    • Reusability: Directly separate a small function for use in other projects.
    • Easy to debug: easy to locate a small function when an error occurs -

From the above data, it can be seen that for the so-called benefits, humans would rather make more use of the script. Although it is necessary to increase the amount of typing by two-thirds, it is better to think of a good name, although it takes more effort to refine it.

Praising functional programming itself is like propagating how frustrated and weak humans are, and fearing that the mighty universe does not know how weak humans are, but this is indeed an effective measure for weak humans to fight against the vast universe, again, any functional program. There is a natural endowment that can be used for the benefit of others. It takes more time to spend more time, and human beings are farther and farther in the abstract way.

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now