[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: MAP (and variants) vs LOOP - Popular opinion observation?

William James

4/11/2015 4:28:00 AM

Nathan Baum wrote:

> Suppose you have
>
> (loop for x in (get-list)
> do (format t "~A~%" x))


Gauche Scheme:

(for-each print (get-list))


>
> and then it turns out you need to print a numeric index. You can do
>
> (loop for x in (get-list)
> for i from 0
> do (format t "~A - ~A~%" i x))
>
> If you start with
>
> (mapc (lambda (x) (format t "~A~%" x)) (get-list))
>
> it seems (to me) that it'd be harder to modify it as needed,
>
> (let ((list (get-list)))
> (mapc (lambda (i x) (format t "~A - ~A" i x))
> (range 0 (length list))
> list))
>
> (I'm assuming the toolkit includes a RANGE utility, or something similar.)


(for-each (cut print <> " - " <>) (lrange 0) (get-list))

Another way:

(use srfi-42)

(do-ec (: x (index i) (get-list)) (print i " - " x))


>
> Then suppose you later need the loop/map to collect some of the values
> under certain conditions. You might have
>
> (loop for x in (get-list)
> for i from 0
> do (format t "~A - ~A~%" i x)
> if (test x)
> collect (foo x))
>
> compared to
>
> (let ((list (get-list)))
> (mapcan (lambda (i x)
> (format t "~A - ~A" i x)
> (if (test x)
> (list (foo x))
> nil))
> (range 0 (length list))
> list))


(filter-map
(^(i x) (print i " - " x) (and (test x) (foo x)))
(lrange 0)
(get-list))


Another way:

(use srfi-42)

(list-ec (: x (index i) (get-list))
(begin (print i " - " x))
(if (test x))
(foo x))
2 Answers

William James

12/3/2015 8:10:00 PM

0

WJ wrote:

> >
> > Then suppose you later need the loop/map to collect some of the values
> > under certain conditions. You might have
> >
> > (loop for x in (get-list)
> > for i from 0
> > do (format t "~A - ~A~%" i x)
> > if (test x)
> > collect (foo x))
> >

Ruby:

get_list.each_with_index.
grep->((x,i)){puts "#{ i } - #{ x }"; test x}{|x,_| foo x}


Testing:

def get_list; ('aa'..'zz').step(36).to_a ; end
def test s; s == s.reverse ; end
def foo x; x.upcase ; end

get_list.each_with_index.
grep->((x,i)){printf "%2d - %s\n", i, x; test x}{|x,_| foo x}

0 - aa
1 - bk
2 - cu
3 - ee
4 - fo
5 - gy
6 - ii
7 - js
8 - lc
9 - mm
10 - nw
11 - pg
12 - qq
13 - sa
14 - tk
15 - uu
16 - we
17 - xo
18 - yy
==>["AA", "EE", "II", "MM", "QQ", "UU", "YY"]


Ocaml:

open List;;
open Printf;;

let accum = ref [] in
iteri (fun i x -> printf "%d - %d\n" i x;
if test x then accum := foo x :: !accum)
(get_list ()) ;
rev !accum ;;

--
I think that we have to remember that the real activists who are doing this are
motivated not by love for humanity, not by love for America or anything else;
they are motivated by hatred for the traditional people and culture of America.
They are motivated by hatred toward the traditional people and culture of the
European countries. --- www.redicecreations.com/radio/2014/09/RIR-140929.php

William James

2/29/2016 8:43:00 PM

0

WJ wrote:

> WJ wrote:
>
> > >
> > > Then suppose you later need the loop/map to collect some of the values
> > > under certain conditions. You might have
> > >
> > > (loop for x in (get-list)
> > > for i from 0
> > > do (format t "~A - ~A~%" i x)
> > > if (test x)
> > > collect (foo x))
> > >
>
> Ruby:
>
> get_list.each_with_index.
> grep->((x,i)){puts "#{ i } - #{ x }"; test x}{|x,_| foo x}
>
>
> Testing:
>
> def get_list; ('aa'..'zz').step(36).to_a ; end
> def test s; s == s.reverse ; end
> def foo x; x.upcase ; end
>
> get_list.each_with_index.
> grep->((x,i)){printf "%2d - %s\n", i, x; test x}{|x,_| foo x}
>
> 0 - aa
> 1 - bk
> 2 - cu
> 3 - ee
> 4 - fo
> 5 - gy
> 6 - ii
> 7 - js
> 8 - lc
> 9 - mm
> 10 - nw
> 11 - pg
> 12 - qq
> 13 - sa
> 14 - tk
> 15 - uu
> 16 - we
> 17 - xo
> 18 - yy
> ==>["AA", "EE", "II", "MM", "QQ", "UU", "YY"]

SML:

Preliminaries:

fun palindrome s = s = String.implode (List.rev (String.explode s));

fun up s = String.map Char.toUpper s;

fun base26 n =
String.implode (List.map (fn x=> Char.chr (x+97))[n div 26, n mod 26]);

fun format n s = List.app print [Int.toString n," - ",s,"\n"];

fun get_vector () = Vector.tabulate (19, fn i=> base26 (i*36));


The loop:

List.rev
(Vector.foldli
(fn (i,s,acc) => (format i s; if palindrome s then up s::acc else acc))
[]
(get_vector ()));

0 - aa
1 - bk
2 - cu
3 - ee
4 - fo
5 - gy
6 - ii
7 - js
8 - lc
9 - mm
10 - nw
11 - pg
12 - qq
13 - sa
14 - tk
15 - uu
16 - we
17 - xo
18 - yy
> val it = ["AA", "EE", "II", "MM", "QQ", "UU", "YY"] : string list

--
Europe is not going to be the monolithic societies that they once were in the
last century.... They are now going into a multicultural mode. Jews will be
resented because of our leading role. --- Barbara Spectre
http://archive.org/download/DavidDuke_videos/HowZionistsDivideAndConquer-fjjsz...