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

Re: YANQ - When to map, when to iterate, when to recurse? And why CLOS?

106 views
Skip to first unread message

WJ

unread,
May 2, 2012, 6:34:55 AM5/2/12
to
Wade Humeniuk wrote:

> John Connors wrote:
>
> > Yet Another Noob Question.
>
> > What are the characteristics of each implementation?
>
> > How can I tell which I should be writing in what context...
>
> > (defun find-indicies (lst tst)
> > (let ((li 0))
> > (labels ((qpred (a)
> > (incf li)
> > (if (funcall tst a)
> > (1- li)
> > nil)))
> > (remove nil (mapcar #'qpred lst)))))
>
> > (defun find-indices (lst tst)
> > (loop
> > for el in lst
> > counting t into index
> > if (funcall tst el)
> > collect index))
>
> You are allergic to loop
>
> (defun find-indices (list test)
> (loop for element in list
> for index from 1
> when (funcall test element) collect index))

Racket:

(define (find-indices alist test)
(for/list ([(x i) (in-indexed alist)]
#:when (test x))
i))

(find-indices '(1 2 3 4 5) odd?)
=> '(0 2 4)

daniel....@excite.com

unread,
May 2, 2012, 10:14:20 AM5/2/12
to
You know, Racket is built on top of Scheme..

I mean, I notice that you are not posting solutions in basic Scheme... I believe the code in such case would look quite similar to the "labels" version above...

(Unless you make use of Scheme's "do" form - and I wouldn't without a macro like the Racket macro above!)

WJ

unread,
Dec 22, 2014, 2:53:24 AM12/22/14
to
Gauche Scheme:

gosh> (use srfi-42)
#<undef>
gosh> (list-ec (: x (index i) '(1 2 3 4 5)) (if (odd? x)) i)
(0 2 4)

WJ

unread,
Mar 24, 2015, 3:02:52 AM3/24/15
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
> An fp nerd could do a little better:
>
> (defun find-indices (list pred)
> (labels ((lp (list n)
> (cond ((null list) '())
> ((funcall pred (car list))
> (cons n (lp (cdr list) (1+ n))))
> (t (lp (cdr list) (1+ n))))))
> (lp list 0)))
>
> I wonder how a real fp nerd (which I'm not) would do it, given a decent
> library like you would get in an fp language like Scheme (real
> implementations, not R5RS), ML, or Haskell.

Gauche Scheme:

(define (find-indices alist test)
(filter-map
(^(x i) (and (test x) i))
alist
(lrange 1)))

WJ

unread,
Apr 25, 2016, 11:34:38 PM4/25/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
OCaml:

let find_indices list test =
let accum = ref [] in
List.iteri
(fun i x -> if test x then accum := i :: !accum)
list ;
List.rev !accum ;;

find_indices [4;8;9;22] (fun n -> 0 == n mod 2);;
===>
[0; 1; 3]

--
Goyim were born only to serve us.... They will work, they will plow, they will
reap. We will sit like an effendi and eat. --- Rabbi Ovadia Yosef
http://archive.org/download/DavidDuke_videos/TopRabbiExposesJewishRacism-cybsdwjezqi.ogv
https://web.archive.org/web/20101020044210/http://www.jpost.com/JewishWorld/JewishNews/Article.aspx?id=191782

WJ

unread,
Jul 21, 2016, 2:01:28 PM7/21/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
R:

Create a vector of random integers:

> sample(-3:5, 22, rep=TRUE) -> v
> v
[1] 5 2 -3 2 4 -1 -2 -3 -3 -1 4 4 -1 0 0 -2 -2 1 2 0 0 3

Find the indices at which v < 0:

> which(v < 0)
[1] 3 6 7 8 9 10 13 16 17

Extract the negative numbers:

> v[which(v < 0)]
[1] -3 -1 -2 -3 -3 -1 -1 -2 -2

--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunkers-5.html
https://www.youtube.com/watch?v=EEl_1HWFRfo

WJ

unread,
Jul 21, 2016, 2:14:29 PM7/21/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
That was regrettable newbie prolixity.

> v<0
[1] FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE
[13] TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
> v[v<0]
[1] -3 -1 -2 -3 -3 -1 -1 -2 -2

--
[H]e was prosecuted ... for "denying" the gas chambers and the six million
figure. In July 1998 a Swiss court sentenced him to 15 months imprisonment,
and to pay a large fine, because of his writings. Rather than serve the
sentence, in August 2000 Graf went into exile. In 2001 he married a Russian
historian in Moscow. www.revisionists.com/revisionists/graf.html

WJ

unread,
Jul 21, 2016, 4:45:05 PM7/21/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Change all values < 0 to 90:

> v[v<0] = 90
> v
[1] 5 2 90 2 4 90 90 90 90 90 4 4 90 0 0 90 90 1 2 0 0 3

0 new messages