[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Another code review perhaps?

William James

8/3/2015 3:45:00 PM

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

He obviously didn't test it even once.

Gauche Scheme:

(use srfi-42 :only (list-ec))
(define (precedes x vec)
(list-ec (:vector y (index i) vec)
(and (equal? x y) (> i 0))
(~ vec (- i 1))))

(precedes 4 #(4 2 3 4 5 4))
===>
(3 5)


>
> > Define iterative and recursive versions of a function that takes an
> > object and a list, and returns a new list in which the object appears
> > between each pair of elements in the original list:
>
> This one is also pretty easy with LOOP:
>
> (defun intersperse (x list)
> (loop for (a . rest) on list
> collect a
> when rest
> collect x))

gosh> (intersperse '+ '(1 2 3))
(1 + 2 + 3)

--
Sweden is already a banana republic, perhaps on its way to becoming an Islamic
republic. Swedish culture is disappearing with astonishing speed in front of
our eyes. If the trend isn't stopped, the Swedish nation will simply cease to
exist in any meaningful way during the first half of this century.
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html
2 Answers

William James

11/29/2015 12:51:00 AM

0

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]

William James

3/31/2016 3:12:00 PM

0

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