[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

nice programming using pattern matching

Taoufik Dachraoui

5/24/2015 7:11:00 PM

Hi

I just implemented a setter "(setf (it x) value)" for the pattern matching tool I wrote a
long time ago; I would like to know if you agree that it is a nice tool for programming

; if first and last elements of the list are equal then set the LAST to 100
(match '(3 1 2 3) ((:and (:match (:or (?x) (_ . :self))) (?x . _)) (setf (it x) 100) it))
=> (3 1 2 100)

; if first and last elements of the list are equal then set the FIRST to 100
(match '(3 1 2 3) ((:and (?x . _) (:match (:or (?x) (_ . :self)))) (setf (it x) 100) it))
=> (100 1 2 3)


;;
? (defun foo (lst s)
(match lst ((:and (:match (:or (?x) (_ . :self))) (?x . _)) (setf (it x) s) it) (_ nil)))
FOO
? (foo '(1 2 3 4 1) 0)
(1 2 3 4 0)
? (foo '(1 2 3 4 5) 0)
NIL

- Taoufik
4 Answers

Taoufik Dachraoui

5/25/2015 7:15:00 PM

0

On Sunday, May 24, 2015 at 9:11:02 PM UTC+2, Taoufik Dachraoui wrote:
> Hi
>
> I just implemented a setter "(setf (it x) value)" for the pattern matching tool I wrote a
> long time ago; I would like to know if you agree that it is a nice tool for programming
>
> ; if first and last elements of the list are equal then set the LAST to 100
> (match '(3 1 2 3) ((:and (:match (:or (?x) (_ . :self))) (?x . _)) (setf (it x) 100) it))
> => (3 1 2 100)
>
> ; if first and last elements of the list are equal then set the FIRST to 100
> (match '(3 1 2 3) ((:and (?x . _) (:match (:or (?x) (_ . :self)))) (setf (it x) 100) it))
> => (100 1 2 3)
>
>
> ;;
> ? (defun foo (lst s)
> (match lst ((:and (:match (:or (?x) (_ . :self))) (?x . _)) (setf (it x) s) it) (_ nil)))
> FOO
> ? (foo '(1 2 3 4 1) 0)
> (1 2 3 4 0)
> ? (foo '(1 2 3 4 5) 0)
> NIL
>
> - Taoufik

How to write the following using loop or iterate:

? (match '(0 ((2 (0 1)) ((3 2) 1))) ((:or (:and (:type atom) (:adjoin test 'eq into ?x)) (:self :self)) x))
(0 2 1 3)

-Taoufik

Taoufik Dachraoui

5/25/2015 7:41:00 PM

0

On Sunday, May 24, 2015 at 9:11:02 PM UTC+2, Taoufik Dachraoui wrote:
> Hi
>
> I just implemented a setter "(setf (it x) value)" for the pattern matching tool I wrote a
> long time ago; I would like to know if you agree that it is a nice tool for programming
>
> ; if first and last elements of the list are equal then set the LAST to 100
> (match '(3 1 2 3) ((:and (:match (:or (?x) (_ . :self))) (?x . _)) (setf (it x) 100) it))
> => (3 1 2 100)
>
> ; if first and last elements of the list are equal then set the FIRST to 100
> (match '(3 1 2 3) ((:and (?x . _) (:match (:or (?x) (_ . :self)))) (setf (it x) 100) it))
> => (100 1 2 3)
>
>
> ;;
> ? (defun foo (lst s)
> (match lst ((:and (:match (:or (?x) (_ . :self))) (?x . _)) (setf (it x) s) it) (_ nil)))
> FOO
> ? (foo '(1 2 3 4 1) 0)
> (1 2 3 4 0)
> ? (foo '(1 2 3 4 5) 0)
> NIL
>
> - Taoufik

? defun foo (e) (match e ((:all-tree (:adjoin test 'eq into ?x)) x)))
FOO
? (foo '(0 ((2 (0 1)) ((3 2) 1))))
(0 2 1 3)

Taoufik Dachraoui

5/25/2015 7:47:00 PM

0

On Sunday, May 24, 2015 at 9:11:02 PM UTC+2, Taoufik Dachraoui wrote:
> Hi
>
> I just implemented a setter "(setf (it x) value)" for the pattern matching tool I wrote a
> long time ago; I would like to know if you agree that it is a nice tool for programming
>
> ; if first and last elements of the list are equal then set the LAST to 100
> (match '(3 1 2 3) ((:and (:match (:or (?x) (_ . :self))) (?x . _)) (setf (it x) 100) it))
> => (3 1 2 100)
>
> ; if first and last elements of the list are equal then set the FIRST to 100
> (match '(3 1 2 3) ((:and (?x . _) (:match (:or (?x) (_ . :self)))) (setf (it x) 100) it))
> => (100 1 2 3)
>
>
> ;;
> ? (defun foo (lst s)
> (match lst ((:and (:match (:or (?x) (_ . :self))) (?x . _)) (setf (it x) s) it) (_ nil)))
> FOO
> ? (foo '(1 2 3 4 1) 0)
> (1 2 3 4 0)
> ? (foo '(1 2 3 4 5) 0)
> NIL
>
> - Taoufik

? (match '(0 ((2 (0 1)) ((3 2) 1))) ((:all-tree (:adjoin test 'eq into ?x)) x))
(0 2 1 3)
? (match '(0 ((2 (0 1)) ((3 2) 1))) ((:all-tree (:collect into ?x)) x))
(0 2 0 1 3 2 1)
? (match '(0 ((2 (0 1)) ((3 2) 1))) ((:all-tree (:count into ?x)) x))
7
? (match '(0 ((2 (0 1)) ((3 2) 1))) ((:all-tree (:sum into ?x)) x))
9
? (match '(0 ((2 (0 1)) ((3 2) 1))) ((:all-tree (:max into ?x)) x))
3
? (match '(0 ((2 (0 1)) ((3 2) 1))) ((:all-tree (:min into ?x)) x))
0

William James

5/26/2015 7:54:00 AM

0

> How to write the following using loop or iterate:
>
> ? (match '(0 ((2 (0 1)) ((3 2) 1)))
> ((:or (:and (:type atom) (:adjoin test 'eq into ?x)) (:self :self)) x))
> (0 2 1 3)


Gauche Scheme:

(delete-duplicates (let go ((x '(0 ((2 (0 1)) ((3 2) 1)))))
(append-map (^x (if (list? x) (go x) (list x))) x)))
===>
(0 2 1 3)

Racket:

(require srfi/1)
(delete-duplicates (flatten '(0 ((2 (0 1)) ((3 2) 1)))))

MatzLisp:

[0, [[2, [0, 1]], [[3, 2], 1]]].flatten.uniq
==>[0, 2, 1, 3]

--
He has nothing but kind sentiments for those who would destroy his home and
family.... He is universally tolerant. He is totally unprejudiced. If he has
any principles, he keeps them well concealed.... He is, to the extent of his
abilities, exactly like the next citizen, who, he trusts, is trying to be
exactly like him: a faceless, characterless putty-man.
--- Father Feeney; "Should Hate Be Outlawed?"