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

Re: tasters wanted

7 views
Skip to first unread message

WJ

unread,
Aug 21, 2016, 3:36:08 AM8/21/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Ken Tilton wrote:

> Ooh! Ooh! Lemme try again!
>
> (defun collect-repeats-simple (sorted-list &key (test 'eql))
> (loop with acc and tail
> for a in sorted-list
> for b in (cdr sorted-list)
>
> if (funcall test a b)
> if acc do (setf tail (rplacd tail (list b)))
> else do (setf acc (list* a (setf tail (list b))))
> else when acc collect acc into result
> and do (setf acc nil)
>
> finally (return (nconc result
> (when acc (list acc))))))
>
> God I love rplaca/d!

OCaml:

let group eql =
List.fold_left
(fun result x ->
match result with
[] -> [[x]]
| (y::ys)::rest ->
if eql y x then (x::y::ys)::rest
else [x]::(y::ys)::rest)
[] ;;

let collect_repeats ?(eql=(=)) list =
group eql list
|> List.filter (fun xs -> List.length xs > 1)
|> List.rev_map List.rev ;;

collect_repeats [];;
===> []
collect_repeats [0];;
===> []
collect_repeats [0;2;2];;
===> [[2; 2]]
collect_repeats [0;2;2;3;4;5;5;5];;
===> [[2; 2]; [5; 5; 5]]
collect_repeats [0;2;2;3;4;5;5;5;6];;
===> [[2; 2]; [5; 5; 5]]
collect_repeats [0;2;2;3;4;5;5;5;6;6];;
===> [[2; 2]; [5; 5; 5]; [6; 6]]
collect_repeats [0;0;2;2;3;4;5;5;5;6;6];;
===> [[0; 0]; [2; 2]; [5; 5; 5]; [6; 6]]
collect_repeats [0;0;2;2;3;4;5;5;5;6;6] ~eql:(fun a b->(a mod 2)=(b mod 2));;
===> [0; 0; 2; 2]; [5; 5; 5]; [6; 6]]


--
In the United States in 2005, 37,460 white females were sexually assaulted or
raped by a black man, while between zero and ten black females were sexually
assaulted or raped by a white man. What this means is that every day in the
United States, over one hundred white women are raped or sexually assaulted by
a black man. -- http://archive.frontpagemag.com/readArticle.aspx?ARTID=26368
0 new messages