Heime <[email protected]> writes:
> On Saturday, March 28th, 2026 at 3:40 AM, Rudolf Adamkovič <[email protected]> wrote:
>
>> Heime <[email protected]> writes:
>>
>> [I forgot to CC the list; sorry for the duplicate message.]
>>
>> >> Is it possible to determine whether an elisp mathematical expression
>> >> is valid before evaluating it?
>>
>> That depends entirely on what you consider a valid mathematical
>> expression. :)
>
> Valid for lisp eval.
>
>>
>> Why not Lisp instead?
>>
>> (string-to-number (calc-eval "10 + 5 * 2")) => 20
>
Note that while "10 + 5 * 2" is valid for ‘eval’ (it
evaluates to a string), it not a valid Emacs Lisp
mathematical expression (it does not evaluate to a
mathematical result).
So, if you are requiring that M-: can evaluate your
mathematical expressions, then the example would be:
(string-to-number (calc-eval '(+ 10 (* 5 2)))) => 20
and
(string-to-number (calc-eval '(+ 10 (* 5 +))))
evaluates to an error that causes Emacs to enter its
debugger, which is part of the original problem for which a
solution is needed.
Also,
(string-to-number (calc-eval '(+ 10 (* x y))))
evaluates to an error. This is not a problem if you do not
expect your mathematical expressions to include variable
names.
Also, examine some invalid algebraic expressions:
(string-to-number (calc-eval '(+ +)))
;;=> 0
(string-to-number (calc-eval '(- -)))
;;=> 0
(string-to-number (calc-eval '(- +)))
;;=> 0
(string-to-number (calc-eval '(+ -)))
;;=> 0
(string-to-number (calc-eval '(* /)))
;;=> 0
So, ‘calc-eval’ evaluates valid expressions, but it has
special rules that accept expressions that Emacs ‘eval’
considers invalid. Also, Emacs’s ‘eval’ considers many
expressions to be valid that are not valid algebraic
expressions, for example, strings.
--
The lyf so short, the craft so long to lerne.
- Geoffrey Chaucer, The Parliament of Birds.
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.