Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: implementing Haskell style flip for Lisp

29 views
Skip to first unread message

WJ

unread,
Mar 27, 2015, 12:09:51 AM3/27/15
to
> > 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))))

Gauche Scheme:

(map (cut - <> 2) (iota 11 2))

===>
(0 1 2 3 4 5 6 7 8 9 10)


Racket:

(require srfi/26) ; cut
(map (cut - <> 2) (range 2 13))
===>
'(0 1 2 3 4 5 6 7 8 9 10)

Another way:

(map (curryr - 2) (range 2 13))
===>
'(0 1 2 3 4 5 6 7 8 9 10)
0 new messages