help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to create a higher order function?


From: Marcin Borkowski
Subject: Re: How to create a higher order function?
Date: Fri, 24 Sep 2021 11:00:37 +0200
User-agent: mu4e 1.1.0; emacs 28.0.50

On 2021-09-21, at 13:27, Leo Butler <leo.butler@umanitoba.ca> wrote:

> Marcin Borkowski <mbork@mbork.pl> writes:
>
>> Hi all,
>>
>> assume that I want a function which "inverts" the meaning of a given
>> predicate.  Something that would turn `<' into `>=' etc.
>>
>> So, here's a better (at least: working) version (B):
>>
>> ;;; -*- lexical-binding: nil; -*-
>> (defun negate (fun)
>>   "Return a function returning the logical opposite of FUN."
>>   `(lambda (&rest args)
>>     (not (apply ,(symbol-function fun) args))))
>
> I think negate would need to be a defmacro in this case, not a defun.

Maybe.  I'm not really convinced.

> This construction, as it is, would return a list, not a lambda function.
>
>  `(foo ,X bar) is equivalent to (list 'foo X 'bar).
>
>>
>>
>> It seems to work fine, even if it's a bit complicated.
>>
>> Of course, the more obvious way to do it is to use the first definition
>> under lexical scope (C):
>>
>> ;;; -*- lexical-binding: t; -*-
>> (defun negate (fun)
>>   "Try to return a function returning the logical opposite of FUN."
>>   (lambda (&rest args)
>>     (not (apply fun args))))
>
> Obviously C is better.
>
> Q: I see you use mu4e. How do you get the code highlighted? (in my Gnus,
> the background is distinctive blue).

??? I don't do anything in particular apart from using a pretty old
version of mu/mu4e.  And code is not highlighted in my emails.

Best,

-- 
Marcin Borkowski
http://mbork.pl



reply via email to

[Prev in Thread] Current Thread [Next in Thread]