On Wed, Aug 08, 2012 at 08:00:26PM -0600, Jim Weirich wrote:
> 
> On Aug 8, 2012, at 1:50 PM, Timothy Baldridge <tbaldri...@gmail.com> wrote:
> 
> > 
> > >>I'm looking for medium-scale examples of using function-generating 
> > >>functions.
> > 
> > 
> > I'm not sure if this is exactly what you're looking for, but this sort of 
> > thing is pretty cool: 
> > 
> > (defn make-point [x y]
> >   (fn [member]
> >     (cond (= member :x) x
> >              (= member :y) y)))
> 
> Nice.  I always enjoyed this variation on the whole make-point theme:
> 
> (defn make-point [x y]
>  (fn [f] (f x y)))
> 
> (defn point-x [pt]
>  (pt (fn [x y] x)))
> 
> (defn point-y [pt]
>  (pt (fn [x y] y)))
> 
> (def pt (make-point 1 2))
> 
> (println  [(point-x pt)
>           (point-y pt)])
> 
> -- 
> -- Jim Weirich
> -- jim.weir...@gmail.com

Very nice. Similarly, I thought it was slick when I saw this idea used
as an example implementation for cons. Funny enough, it's exactly the
same except for the names.

(defn cons [a b]
  (fn [f] (f a b)))

(defn car [xs]
  (xs (fn [a b] a)))

(defn cdr [xs]
  (xs (fn [a b] b)))

Robert

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to