[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Am I missing something about (loop ... maximizing ...) ?

William James

10/22/2015 11:39:00 PM

> > 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

--
The Authoritarian Personality extends beyond the attempt to pathologize
cohesive gentile groups to pathologizing adaptive gentile behavior in general.
The principal intellectual difficulty is that behavior that is critical to
Judaism as a successful group evolutionary strategy is conceptualized as
pathological in gentiles. --- Dr. Kevin MacDonald; "The Frankfurt School of
Social Research and the Pathologization of Gentile Group Allegiances"
3 Answers

William James

3/27/2016 7:57:00 AM

0

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

--
"To destroy a nation, convince its members that nationalism is evil.
To destroy a race, convince its members that racialism is evil."

William James

3/27/2016 8:09:00 AM

0

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

We can make it shorter by using "max" if we put
the value first and the element second.

let find_maximizing_item (x::xs) func =
List.fold_left
(fun (v,e) element -> max (v,e) (func element, element))
(func x, x)
xs ;;

--
According to a 1947 Gallup poll, 40 percent of Danes canvassed had been
outspokenly sympathetic toward Germany. Just 32 percent had felt hostile.
--- Richard Tedor, Hitler's Revolution

William James

3/27/2016 8:32:00 PM

0

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