From: jra
Subject: implementing Haskell style flip for Lisp
Date: 
Message-ID: <1138393082.779807.218840@f14g2000cwb.googlegroups.com>
How does one implement, in Lisp, a function that returns a function
that takes the arguments of the given function in reverse order?

For example (I'm pretty sure the second one is incorrect Lisp syntax):

(nth 2 '(a b c))
-> b
((flip nth) '(a b c)  2)
-> b

the implementation in Haskell is:

flip :: (a -> b -> c) -> (b -> a -> c)
flip f x y = f y x

these are my first attempts for Lisp:

(defun flip (function)
  (lambda (&rest args)
    (apply function (reverse args))))

(defmacro flip (function)
  `(apply ,function (reverse args)))

I don't think that these worked, it may be that I wasn't calling them
corectly.

(#'(flip nth) '(a b c) 2)  ???
((flip nth) '(a b c) 2) ???
(apply (flip nth) '(a b c) 2) ???
((flip #'nth) '(a b c) 2) ???

Thanks -Jason

From: ··············@hotmail.com
Subject: Re: implementing Haskell style flip for Lisp
Date: 
Message-ID: <1138393930.478857.161560@g14g2000cwa.googlegroups.com>
jra wrote:
> How does one implement, in Lisp, a function that returns a function
> that takes the arguments of the given function in reverse order?
>
> For example (I'm pretty sure the second one is incorrect Lisp syntax):
>
> (nth 2 '(a b c))
> -> b
> ((flip nth) '(a b c)  2)
> -> b

Not sure what you mean by "Lisp", but the use of a function call as the
first element of a form is allowed in Scheme, not Common Lisp.

Also, Common Lisp's NTH counts from zero

(nth 2 '(a b c) ==> C

> the implementation in Haskell is:
>
> flip :: (a -> b -> c) -> (b -> a -> c)
> flip f x y = f y x

I'll take your word for it.

> these are my first attempts for Lisp:
>
> (defun flip (function)
>   (lambda (&rest args)
>     (apply function (reverse args))))
>

This seems to be exactly right.

> (defmacro flip (function)
>   `(apply ,function (reverse args)))

If you want a function, you don't want a macro. And vice-versa.

> I don't think that these worked, it may be that I wasn't calling them
> corectly.
>
> (#'(flip nth) '(a b c) 2)  ???
> ((flip nth) '(a b c) 2) ???
> (apply (flip nth) '(a b c) 2) ???
> ((flip #'nth) '(a b c) 2) ???

In Common Lisp

(funcall (flip #'nth) '(a b c) 2) ==> 2

Note that (flip #'nth) is referring to the function named NTH, as
opposed to the function stored in the variable named NTH. (Scheme does
not distinguish these.) Your third example tries to retrieve the value
of the variabled named NTH and pass it to FLIP. Usually, you will get
an error saying something like "Variable NTH is unbound."

That expression evaluates to a function which is the "fliipped version"
of NTH, and is then called with the arguments '(a b c) and 2.

Alternatively,

(apply (flip #'nth) '(a b c) (list 2))

or 

(apply (flip #'nth) '(a b c) (list 2))
From: Brian Downing
Subject: Re: implementing Haskell style flip for Lisp
Date: 
Message-ID: <6ovCf.758975$xm3.62082@attbi_s21>
In article <························@f14g2000cwb.googlegroups.com>,
jra <·········@gmail.com> wrote:
> How does one implement, in Lisp, a function that returns a function
> that takes the arguments of the given function in reverse order?
> 
> these are my first attempts for Lisp:
 
> (defun flip (function)
>   (lambda (&rest args)
>     (apply function (reverse args))))

The above is correct.  To call it, you want FUNCALL and FUNCTION (i.e.
#').

CL-USER> (funcall (flip #'nth) '(a b c) 2)
C

"Call the function returned by calling the function FLIP on the function
named by NTH on the arguments '(A B C) and 2."

-bcd
-- 
*** Brian Downing <bdowning at lavos dot net> 
From: jra
Subject: Re: implementing Haskell style flip for Lisp
Date: 
Message-ID: <1138404609.169213.277850@f14g2000cwb.googlegroups.com>
Perfect!

funcall was the concept I was missing. Is this the correct way to
implement this concept in Common Lisp? Does Common Lisp already have
this built in onder another name/concept? Is it better to use a
function or a macro?

Thanks -jra
From: Wade Humeniuk
Subject: Re: implementing Haskell style flip for Lisp
Date: 
Message-ID: <bkPCf.140920$6K2.26481@edtnps90>
jra wrote:
> Perfect!
> 
> funcall was the concept I was missing. Is this the correct way to
> implement this concept in Common Lisp? Does Common Lisp already have
> this built in onder another name/concept? Is it better to use a
> function or a macro?
> 
> Thanks -jra
> 

I would do it this way,  FLIP would surround the whole
form (not just the function) and the macro would transform
the whole function-form).
I would stay away of creating a anonymous function that can be
applied and funcalled.  (It looks error prone to be flipping
arguments when you are doing an apply.)

How does Haskell code typically use the flip code??  I am
not sure of its applicable use.

(defmacro flip (function-form)
   `(,(car function-form) ,@(reverse (cdr function-form))))

(defmacro flip-apply (function &rest args)
   `(apply ,function ,@(reverse args)))

(defmacro flip-funcall (function &rest args)
   `(funcall ,function ,@(reverse args)))

CL-USER 5 > (flip (nth '(a b c) 2))
C

CL-USER 6 > (flip-funcall 'nth '(a b c) 2)
C

CL-USER 7 > (flip-apply 'nth '((a b c)) 2)
C

Wade
From: Jason Kantz
Subject: Re: implementing Haskell style flip for Lisp
Date: 
Message-ID: <1138483983.796848.294370@o13g2000cwo.googlegroups.com>
> Wade Humeniuk writes:
> How does Haskell code typically use the flip code??  I am
> not sure of its applicable use.

Here's an example,
CL-USER> (mapcar (bind (flip #'-) 2) (enum-from-to 2 12))
(0 1 2 3 4 5 6 7 8 9 10)


(defun enum-from-to (lo hi)
  (loop for x from lo to hi collect x))

(defun bind (f &rest bound-args)
  #'(lambda (&rest args)
      (apply f (append bound-args args))))

(defun flip (f)
  #'(lambda (&rest args)
      (apply f (reverse args))))
From: Marcin 'Qrczak' Kowalczyk
Subject: Re: implementing Haskell style flip for Lisp
Date: 
Message-ID: <87r76sar8d.fsf@qrnik.zagroda>
Wade Humeniuk <··················@telus.net> writes:

> How does Haskell code typically use the flip code??

As a partial application to the second argument, to create a function
which is usually passed as a value, rarely bound to a name. Example:

reverse = foldl (flip (:)) []

Nobody uses it when both arguments are specified, because then it's
just equivalent to putting them in the opposite order and doesn't buy
anything.

-- 
   __("<         Marcin Kowalczyk
   \__/       ······@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/
From: Marcin 'Qrczak' Kowalczyk
Subject: Re: implementing Haskell style flip for Lisp
Date: 
Message-ID: <87oe1waqvw.fsf@qrnik.zagroda>
Wade Humeniuk <··················@telus.net> writes:

> How does Haskell code typically use the flip code??

To make a function which is passed as a value, or as a partial application
to the second argument, or to make a function and bind it to a name.
Examples (in the above order, which is probably the order of popularity):

reverse = foldl (flip (:)) []
new_hfs = map (flip apPap args) hfs
delete = flip delFromFM

Nobody uses it when both arguments are specified, because then it's
just equivalent to putting them in the opposite order and doesn't buy
anything. Except when used in combination with the $ operator
(f $ x = f x) which allows to omit parens around the last argument:

   flip mapM_ mods $ \m -> ...
which is the same as
   flip mapM_ mods (\m -> ...)
i.e.
   mapM_ (\m -> ...) mods
but allows the "..." to extend far to the right without closing parens.

-- 
   __("<         Marcin Kowalczyk
   \__/       ······@knm.org.pl
    ^^     http://qrnik.knm.org.pl/~qrczak/