William James
3/27/2016 8:32:00 PM
WJ wrote:
> WJ wrote:
>
> > > > Is there a way to add more keywords to LOOP so that I can add a
> > > > "which-maximizes" accumulator? (I'm currently using CLISP 2.35 but
> > > > I'm interested in how any CL makes this work.)
> > >
> > > With enough loop keywords you can do anything.
> > >
> > > (defun find-maximizing-item (fn list)
> > > (loop with which-element
> > > for element in list
> > > as previous = nil then maximum
> > > maximizing (funcall fn element) into maximum
> > > when previous when (> maximum previous)
> > > do (setq which-element element)
> > > finally (return (values which-element maximum))))
> >
> > Gauche Scheme:
> >
> > (use gauche.collection)
> >
> > gosh> (find-max '(0 2 -9 4))
> > 4
> > gosh> (find-max '(0 2 -9 4) :key abs)
> > -9
>
> OCaml:
>
> let find_maximizing_item (x::xs) func =
> List.fold_left
> (fun (k,v) item ->
> let value = func item in
> if value > v then (item, value) else (k,v))
> (x, func x)
> xs ;;
>
> # find_maximizing_item [0;2;-8;7;5] abs ;;
> - : int * int = -8, 8
Let's use piping. OCaml 4.01 already has |>.
(* Pipe 2 arguments received as a tuple. *)
let (|>>) (x,y) f = f x y ;;
(* Tuple of head and tail of list. *)
let hd_tl (x::xs) = (x,xs) ;;
open List ;;
[0;2;-8;7;5]
|> map (fun x -> (abs x, x))
|> hd_tl
|>> fold_left max ;;
===>
(8, -8)
Now, isn't that a lot more straightforward than the CL
(COBOL-Like) version?
--
If confirmed, Garland would be the fourth Jewish justice on the nation's
highest court, which is comprised entirely of Jews and Catholics. The three
current Jewish members of the Supreme Court are Ruth Bader Ginsburg, Elana
Kagan, and Stephen Breyer.
www.jta.org/2016/03/16/news-opinion/united-states/obama-to-name-jewish-federal-judge-to-supreme-court