[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: one small function

William James

6/16/2015 8:11:00 PM

Nils Goesche wrote:

> > ok, lets say that we impliment that into a function like this:
> >
> > (defun find-position (word list)
> > (let ((pos (position word list :key #'car)))
> > (and pos (1+ pos))))
> >
> > and we modify our list:
> >
> > (setf my-list
> > '((titi tata) (toto) (titi) (toto) (tada)))
> >
> > to contain several '(toto)s it will still return only the position of
> > the first one
> > 2
> > whereas it should give us
> > 2 4
>
> Ok, how about
>
> (defun find-position (word list)
> (loop for sub in list
> for index from 1
> when (eq word (car sub))
> collect index))

Gauche Scheme:

(use srfi-42 :only (list-ec))

(define (find-positions word data)
(list-ec (:list sub (index i) data)
(if (eq? word (car sub)))
(+ 1 i)))

(find-positions 'toto '((titi tata) (toto) (titi) (toto) (tada)))
===>
(2 4)

Another way:

(define (find-positions word data)
(filter-map
(^(x i) (and (eq? (car x) word) i))
data
(lrange 1)))


--
The curse of man, and the cause of nearly all his woe, is his
stupendous capacity for believing the incredible.
--- H. L. Mencken
3 Answers

William James

11/28/2015 12:24:00 AM

0

WJ wrote:

> Nils Goesche wrote:
>
> > > ok, lets say that we impliment that into a function like this:
> > >
> > > (defun find-position (word list)
> > > (let ((pos (position word list :key #'car)))
> > > (and pos (1+ pos))))
> > >
> > > and we modify our list:
> > >
> > > (setf my-list
> > > '((titi tata) (toto) (titi) (toto) (tada)))
> > >
> > > to contain several '(toto)s it will still return only the position of
> > > the first one
> > > 2
> > > whereas it should give us
> > > 2 4
> >
> > Ok, how about
> >
> > (defun find-position (word list)
> > (loop for sub in list
> > for index from 1
> > when (eq word (car sub))
> > collect index))

Ocaml:

open List;;

let find_positions word data =
data |>
mapi (fun i x -> if word = hd x then [i+1] else [])
|> concat ;;

find_positions "toto"
[["titi"; "tata"]; ["toto"]; ["titi"]; ["toto"]; ["tada"]];;

===>
[2; 4]

--
The curse of man, and the cause of nearly all his woe, is his
stupendous capacity for believing the incredible.
--- H. L. Mencken

William James

2/3/2016 8:51:00 PM

0

WJ wrote:

> Nils Goesche wrote:
>
> > > ok, lets say that we impliment that into a function like this:
> > >
> > > (defun find-position (word list)
> > > (let ((pos (position word list :key #'car)))
> > > (and pos (1+ pos))))
> > >
> > > and we modify our list:
> > >
> > > (setf my-list
> > > '((titi tata) (toto) (titi) (toto) (tada)))
> > >
> > > to contain several '(toto)s it will still return only the position of
> > > the first one
> > > 2
> > > whereas it should give us
> > > 2 4
> >
> > Ok, how about
> >
> > (defun find-position (word list)
> > (loop for sub in list
> > for index from 1
> > when (eq word (car sub))
> > collect index))

MatzLisp (Ruby):

def find_positions word, array
array.each_with_index.select{|(s),i| s==word}.map &:last
end

find_positions :toto, [[:titi,:tata],[:toto],[:titi],[:toto],[:tada]]
===>
[1, 3]

--
Blacks are an estimated 39 times more likely to commit a violent crime
against a white than vice versa, and 136 times more likely to commit robbery.
www.colorofcrime.com/2005/10/the-color-of-crime-2005/

William James

4/11/2016 10:51:00 PM

0

WJ wrote:

> Nils Goesche wrote:
>
> > > ok, lets say that we impliment that into a function like this:
> > >
> > > (defun find-position (word list)
> > > (let ((pos (position word list :key #'car)))
> > > (and pos (1+ pos))))
> > >
> > > and we modify our list:
> > >
> > > (setf my-list
> > > '((titi tata) (toto) (titi) (toto) (tada)))
> > >
> > > to contain several '(toto)s it will still return only the position of
> > > the first one
> > > 2
> > > whereas it should give us
> > > 2 4
> >
> > Ok, how about
> >
> > (defun find-position (word list)
> > (loop for sub in list
> > for index from 1
> > when (eq word (car sub))
> > collect index))
>
> Gauche Scheme:
>
> (use srfi-42 :only (list-ec))
>
> (define (find-positions word data)
> (list-ec (:list sub (index i) data)
> (if (eq? word (car sub)))
> (+ 1 i)))
>
> (find-positions 'toto '((titi tata) (toto) (titi) (toto) (tada)))
> ===>
> (2 4)
>
> Another way:
>
> (define (find-positions word data)
> (filter-map
> (^(x i) (and (eq? (car x) word) i))
> data
> (lrange 1)))

OCaml:

open List;;

let find_positions word list =
concat
(mapi (fun index (car::cdr) -> if car=word then [index] else [])
list);;

find_positions "hi" [["hi";"me"];["it"];["hi"]];;
===>
[0; 2]


--
[T]he organized Jewish community throughout the West has been a major force in
placing penalties on speech related to race, ethnicity, and immigration.
www.theoccidentalobserver.net/2016/03/could-it-happen-here/