William James
3/31/2016 3:12:00 PM
WJ wrote:
> WJ wrote:
>
> > Arthur Lemmens wrote:
> >
> > > > Define iterative and recursive versions of a function that takes an
> > > > object x and a vector v, and returns a list of all the objects that
> > > > immediately precede x in v.
> > >
> > > Graham doesn't like LOOP, but I do. So here's a LOOP-version:
> > >
> > > (defun precedes (object vector)
> > > (loop for x across vector
> > > and i from 0
> > > when (and (equal x object) (> i 0))
> > > collect (elt vector (1-i))))
>
>
> Look at that:
>
> (1-i)
>
> Don't you think that that should be:
>
> (1- i)
>
> or
>
> (- i 1)
>
> ?
>
> Consider the incredible arrogance and stupidity of one who
> would post code like that without testing it.
>
> Ocaml:
>
> let array_filter_segments size func array =
> let result = ref [] in
> for i = (Array.length array) - size downto 0 do
> match func (Array.sub array i size) with
> Some x -> result := x::!result
> | None -> ()
> done;
> !result ;;
>
> let precedes item vector =
> array_filter_segments 2
> (fun [|a;b|] -> if item=b then Some a else None)
> vector ;;
>
> precedes 4 [|4; 2; 3; 4; 5; 4|];;
>
> [3; 5]
Another way:
let precedes item vector =
let result = ref [] in
Array.iteri
(fun i x ->
if i > 0 && x = item then result := vector.(i-1) :: !result)
vector;
List.rev !result ;;
--
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