[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: the FIND function applied to cons cells

William James

4/7/2015 8:57:00 AM

Harald Hanche-Olsen wrote:

> | (defun on-member-if (predicate list &key (key #'(lambda (x) x))
> | (loop for x on list
> | when (funcall predicate (funcall key x))
> | return x))
>
> Minor nit: #'(lambda (x) x) is also known as #'identity.

Scheme:

(define-syntax scan-tails
(syntax-rules ()
([_ var expr seq]
(do ((var seq (cdr var)))
((or (null? var) expr) var)))))

(scan-tails x (eqv? 5 (cadr x)) '(2 3 4 5 6))
===>
(4 5 6)

(scan-tails x (eqv? 5 (car x)) '(2 3 4 5 6))
===>
(5 6)

(scan-tails x (eqv? 9000 (car x)) '(2 3 4 5 6))
===>
()

(scan-tails x (> 20 (apply + x)) '(9 8 7 6 5 4 3 2))
===>
(5 4 3 2)
2 Answers

William James

12/4/2015 12:22:00 AM

0

WJ wrote:

> Harald Hanche-Olsen wrote:
>
> > > (defun on-member-if (predicate list &key (key #'(lambda (x) x))
> > > (loop for x on list
> > > when (funcall predicate (funcall key x))
> > > return x))
> >
> > Minor nit: #'(lambda (x) x) is also known as #'identity.
>
> Scheme:
>
> (define-syntax scan-tails
> (syntax-rules ()
> ([_ var expr seq]
> (do ((var seq (cdr var)))
> ((or (null? var) expr) var)))))
>
> (scan-tails x (eqv? 5 (cadr x)) '(2 3 4 5 6))
> ===>
> (4 5 6)
>
> (scan-tails x (eqv? 5 (car x)) '(2 3 4 5 6))
> ===>
> (5 6)
>
> (scan-tails x (eqv? 9000 (car x)) '(2 3 4 5 6))
> ===>
> ()
>
> (scan-tails x (> 20 (apply + x)) '(9 8 7 6 5 4 3 2))
> ===>
> (5 4 3 2)

Ocaml:

open List;;

let rec scan_tails test = function
[] -> []
| xs -> if test xs then xs else scan_tails test (List.tl xs) ;;

scan_tails (fun x -> 5 = nth x 1) [2;3;4;5;6] ;;
[4; 5; 6]

scan_tails (fun x -> 5 = hd x) [2;3;4;5;6] ;;
[5; 6]

scan_tails (fun x -> 9000 = hd x) [2;3;4;5;6] ;;
[]

scan_tails (fun x -> 20 > fold_left (+) 0 x) [9;8;7;6;5;4;3;2];;
[5; 4; 3; 2]

--
In Sweden, the leadership seems to show Europe's most extensive cultural
self-denial, or rather -- a deeply felt self-loathing, which many feel is the
perfect recipe for driving a peaceful nation to destruction.
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html

Kaz Kylheku

12/4/2015 2:55:00 AM

0

On 2015-12-04, WJ <w_a_x_man@yahoo.com> wrote:
> scan_tails (fun x -> 20 > fold_left (+) 0 x) [9;8;7;6;5;4;3;2];;
> [5; 4; 3; 2]


;; filter complex numbers out of list:

(mapcan (lambda (x) (if (complexp x) (list x)))
'(1 2/3 4.0 #c(5 1) #c(0 0) -13 #c(4.0 3.1) 9))

-> (#C(5 1) #C(4.0 3.1))

;; equivalently:

(remove-if-not #'complexp '(1 2/3 4.0 #c(5 1) #c(0 0) -13 #c(4.0 3.1) 9))

-> (#C(5 1) #C(4.0 3.1))

OCaml?