all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Function binders in Elisp?
@ 2005-04-17 17:47 PT
  2005-04-18 16:26 ` rgb
  0 siblings, 1 reply; 5+ messages in thread
From: PT @ 2005-04-17 17:47 UTC (permalink / raw)


For predicates I usually use lambda functions. For example:

   (require 'cl)
   (remove-if (lambda (x) (> x 2))
              '(1 2 3 4))

I'm not a lisp guru, therefore I often wonder if there are standard binder  
functions in emacs lisp like for example bind2nd in C++ STL which  
transforms a binary function into an unary function:

   find_if(L.begin(), L.end(), bind2nd(greater<int>(), 2)) // C++

So that I can write something like this:

   (remove-if (bind-second '< 2)
              '(1 2 3 4))

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Function binders in Elisp?
  2005-04-17 17:47 Function binders in Elisp? PT
@ 2005-04-18 16:26 ` rgb
  2005-04-18 18:15   ` PT
  0 siblings, 1 reply; 5+ messages in thread
From: rgb @ 2005-04-18 16:26 UTC (permalink / raw)



PT wrote:
> For predicates I usually use lambda functions. For example:
>
>    (require 'cl)
>    (remove-if (lambda (x) (> x 2))
>               '(1 2 3 4))
>
> I'm not a lisp guru, therefore I often wonder if there are standard
binder
> functions in emacs lisp like for example bind2nd in C++ STL which
> transforms a binary function into an unary function:
>
>    find_if(L.begin(), L.end(), bind2nd(greater<int>(), 2)) // C++
>
> So that I can write something like this:
>
>    (remove-if (bind-second '< 2)
>               '(1 2 3 4))

So you are looking to do this?

(defmacro bind-second (first &rest others)
  `(lambda (x) (,first x ,@others)))

   (remove-if (bind-second > 2)
              '(1 2 3 4))

I'd think that would make the code a bit more confusing to read.
But maybe it's just me.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Function binders in Elisp?
  2005-04-18 16:26 ` rgb
@ 2005-04-18 18:15   ` PT
  2005-04-18 20:20     ` Thien-Thi Nguyen
  2005-04-18 22:45     ` Pascal Bourguignon
  0 siblings, 2 replies; 5+ messages in thread
From: PT @ 2005-04-18 18:15 UTC (permalink / raw)


On Mon, 18 Apr 2005 18:26:12 +0200, rgb <rbielaws@i1.net> wrote:
>
> So you are looking to do this?
>
> (defmacro bind-second (first &rest others)
>   `(lambda (x) (,first x ,@others)))
>
>    (remove-if (bind-second > 2)
>               '(1 2 3 4))
>
> I'd think that would make the code a bit more confusing to read.
> But maybe it's just me.

In very simple cases it might be simpler than writing those lambda  
functions. STL introduced functional programming paradigms in C++, that's  
why I thought there is a standard way in Lisp to do it and the STL  
developers simply implemented the same function binders in C++.

I know I can write my own macros to do that, but a standardized way would  
be better, because it would be recognized by other Lisp programmers too.  
 From your answer it's clear there are no such standard macros in (e)lisp,  
so it's not really worth the trouble, because it would only make my  
programs harder to read for others.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Function binders in Elisp?
  2005-04-18 18:15   ` PT
@ 2005-04-18 20:20     ` Thien-Thi Nguyen
  2005-04-18 22:45     ` Pascal Bourguignon
  1 sibling, 0 replies; 5+ messages in thread
From: Thien-Thi Nguyen @ 2005-04-18 20:20 UTC (permalink / raw)


PT <mailshield.gg@mailnull.com> writes:

> I thought there is a standard way in Lisp

you are looking for closures.
grep around for `lexical-let'.

thi

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Function binders in Elisp?
  2005-04-18 18:15   ` PT
  2005-04-18 20:20     ` Thien-Thi Nguyen
@ 2005-04-18 22:45     ` Pascal Bourguignon
  1 sibling, 0 replies; 5+ messages in thread
From: Pascal Bourguignon @ 2005-04-18 22:45 UTC (permalink / raw)


PT <mailshield.gg@mailnull.com> writes:

> On Mon, 18 Apr 2005 18:26:12 +0200, rgb <rbielaws@i1.net> wrote:
> >
> > So you are looking to do this?
> >
> > (defmacro bind-second (first &rest others)
> >   `(lambda (x) (,first x ,@others)))
> >
> >    (remove-if (bind-second > 2)
> >               '(1 2 3 4))
> >
> > I'd think that would make the code a bit more confusing to read.
> > But maybe it's just me.
> 
> In very simple cases it might be simpler than writing those lambda
> functions. STL introduced functional programming paradigms in C++,
> that's  why I thought there is a standard way in Lisp to do it and the
> STL  developers simply implemented the same function binders in C++.

Well, what is clear and recognized by all lisp programmer is:

(remove-if (lambda (x) (< 2 x)) '(1 2 3 4))  --> (1 2)


Also there's this notion of currying. 
http://www.cs.oberlin.edu/classes/dragn/labs/combinators/combinators11.html

In Common Lisp you'd write:

(defun curry (f) (lambda (x) (lambda (y) (funcall f x y))))

(funcall (funcall (curry '+) 2) 3) --> 5
(remove-if (funcall (curry '<) 2) '(1 2 3 4))  --> (1 2)

It's nicer in scheme.  It doesn't work in emacs lisp.

Despite the awkwardness of the notation in Common Lisp, it migh be
better recognized than bind-second...


> I know I can write my own macros to do that, but a standardized way
> would  be better, because it would be recognized by other Lisp
> programmers too.  From your answer it's clear there are no such
> standard macros in (e)lisp,  so it's not really worth the trouble,
> because it would only make my  programs harder to read for others.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Grace personified,
I leap into the window.
I meant to do that.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-04-18 22:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-04-17 17:47 Function binders in Elisp? PT
2005-04-18 16:26 ` rgb
2005-04-18 18:15   ` PT
2005-04-18 20:20     ` Thien-Thi Nguyen
2005-04-18 22:45     ` Pascal Bourguignon

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.