Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

#'reduce

瀏覽次數:9 次
跳到第一則未讀訊息

Robert L.

未讀,
2017年12月9日 凌晨2:09:552017/12/9
收件者:
> >> (reduce #'max '("This" "is" "a" "test" ".") :key #'length)
> >>
> >
> >Reduce doesn't accept :KEY according to CLtL2. In fact, if you
> >look at REDUCE more closely, you'll see that :KEY wouldn't make sense.
> >
> >Instead of REDUCE, try the following:
> >
> >(APPLY #'MAX (MAPCAR #'LENGTH '("This" "is" "a" "test" ".")))
> >
> >Even more efficient would be:
> >
> >(LET ((max 0))
> > (DOLIST (elt '("This" "is" "a" "test" "."))
> > (SETF max (MAX max (LENGTH elt))))
> > max)
> >
>
> Macintosh Common Lisp (MCL) accepts the :key keyword parameter:
> ? (reduce #'max '("This" "is" "a" "test" ".") :key #'length)
> 4
>
> You can also use loop to make it clearer:
> ? (loop for el in '("This" "is" "a" "test" ".")
> maximizing (length el))
> 4

(require srfi/42) ; max-ec

(max-ec (:list x '("This" "is" "a" "test")) (string-length x))

===>
4


--
[T]hey always try to undermine our sense of identity ... while at the same time
they have ... stories, even in the New York Times, how Jews are ... like fourth
or fifth cousins.... It has just come out ... DNA testing companies ... admit
adding fake African ancestry to White profiles in order to screw with
"racists".... When you look at the people who own these companies, they're not
on our side---they're here to destroy us. http://archive.org/details/nolies
https://archive.org/details/Duke.20171208
0 則新訊息