Monday, October 27, 2014

Elisp: String Compare (Less than)

Less Than

There are many elisp functions for comparisons. The "less than" sign < is a mathematical sign to compare inequality. Elisp has the function < to compare Number or Marker.

(< 1 2)
t
(< 2 1)
nil

It is obvious enough that no explanation is needed.

String Less Than

Elisp also has a "less than" function for Strings, called string-lessp.

(string-lessp "abc" "def")
t
(string-lessp "abc" "ab")
nil

It is a predicate function, meaning that it returns t if the first string, "abc" in the first example, is less than the second strings "def", or nil otherwise.

Elisp have a convention, as noted in elisp info, to end a predicate function with letter "p". Thus, following this convention, string-lessp ends with "p".

Elisp does have string< as an alias for string-lessp. So that you can write the above example:

(string< "abc" "def")
t
(string< "abc" "ab")
nil

"<" vs "p" vs "-p"

It seems like a majority of "less than" functions follow this "lessp" convention, however, there are other functions in Emacs. Here is an incomplete list of "less than" functions I have got from my Emacs with M-x apropos <$\|lessp$\|less-p$.

What functions do you have in your Emacs?

Incomplete List of "less than" functions

  • <
  • <<
  • bibtex-lessp
  • byte-string<
  • coding-system-lessp
  • customize-version-lessp
  • elmo-time-less-p
  • elmo-time<
  • file-attributes-lessp
  • gnus-pseudos<
  • gnus-string<
  • ido-file-extension-lessp
  • ido-file-lessp
  • mailcap-viewer-lessp
  • org-entries-lessp
  • org-table-formula-less-p
  • org-time<
  • recentf-string-lessp
  • s-less-p
  • skk-string-lessp-in-coding-system
  • skk-string<
  • string-lessp
  • string<
  • time-less-p
  • version-list-<
  • version<

No comments :