Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Do something unusual today. Pay a bill.


comp / comp.lang.lisp / re: matrix operations

SubjectAuthor
* re: matrix operationsB. Pym
`- Re: matrix operationsKaz Kylheku

1
Subject: re: matrix operations
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Wed, 3 Jul 2024 01:32 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: re: matrix operations
Date: Wed, 3 Jul 2024 01:32:54 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <v629o5$1sdhf$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Wed, 03 Jul 2024 03:32:54 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="249a2e1b4562ba977d09d2e81681a526";
logging-data="1979951"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/RNTnHEs/vU5gKpyow5pxR"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:RPc65CPkUBdMvV4Ug1WLSMq3kaU=
View all headers

Barry Margolin wrote:

> Zachary Turner <ztur...@bindview.com> wrote:
> >I've got a 3x3 matrix, stored as ((r1c1 r1c2 r1c3) (r2c1 r2c2 r2c3) (r3c1
> >r3c2 r3c3)). I want to check if any of the columns have the same three
> >elements. It's easy for rows, I can just use
> >(or
> > (every #'equal (car matrix))
> > (every #'equal (cadr matrix))
> > (every #'equal (caddr matrix)))
>
> Actually, that doesn't work. EVERY passes a single element to the test
> function, but EQUAL requires two arguments. What you need is:
>
> (or (every #'(lambda (x) (equal x (caar matrix)))
> (cdar matrix))
> (every #'(lambda (x) (equal x (caadr matrix)))
> (cdadr matrix))
> (every #'(lambda (x) (equal x (caaddr matrix)))
> (cdaddr matrix)))

That's hideous.

>
> >My questions are:
> >a) Can I do this more elegantly using a mapxxx function?
>
> (defun (all-elements-equal (list)
> (destructuring-bind (head . tail) list
> (every #'(lambda (x) (equal x head))
> tail))))

"#'(lambda" shows that he doesn't understand
the lambda macro.

Scheme (Gauche or Racket)

;; Racket needs this:
(require srfi/1) ;; every

(define (all-elements-equal lst)
(or (null? lst)
(every equal? lst (cdr lst))))

Subject: Re: matrix operations
From: Kaz Kylheku
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Wed, 3 Jul 2024 02:41 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-1753@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.lisp
Subject: Re: matrix operations
Date: Wed, 3 Jul 2024 02:41:20 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 94
Message-ID: <20240702191750.748@kylheku.com>
References: <v629o5$1sdhf$1@dont-email.me>
Injection-Date: Wed, 03 Jul 2024 04:41:21 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="30e244ac737476d78edd2cd6194d0a15";
logging-data="2115877"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Q1h5HDIeyus82FAiapxOvUR9rkUvF3ic="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:LGFmIhxh0sGGBMj0vb6TAoWppi0=
View all headers

On 2024-07-03, B. Pym <No_spamming@noWhere_7073.org> wrote:
> Barry Margolin wrote:
>
>> Zachary Turner <ztur...@bindview.com> wrote:
>> >I've got a 3x3 matrix, stored as ((r1c1 r1c2 r1c3) (r2c1 r2c2 r2c3) (r3c1
>> >r3c2 r3c3)). I want to check if any of the columns have the same three
>> >elements. It's easy for rows, I can just use
>> >(or
>> > (every #'equal (car matrix))
>> > (every #'equal (cadr matrix))
>> > (every #'equal (caddr matrix)))
>>

If it didn't have to scale to larger matrices, I'd probaby just do:

(defun has-column-3x3 (matrix)
(if-match @(or ((@x . @nil)
(@x . @nil)
(@x . @nil))
((@nil @x . @nil)
(@nil @x . @nil)
(@nil @x . @nil))
((@nil @nil @x)
(@nil @nil @x)
(@nil @nil @x)))
matrix
t))

(defun has-column-3x3 (matrix)
(if-match @(or ((@x @x @xl) . @nil)
(@nil (@x @x @x) . @nil)
(@nil @nil (@x @x @x)))
matrix
t))

(defun has-diagonal-3x3 (matrix)
(if-match @(or ((@x . @nil)
(@nil @x . @nil)
(@nil @nil @x))
((@nil @nil @x)
(@nil @x . @nil)
(@x . @nil)))
matrix
t))

If we want to test for a specific value of x (e.g. #\X or #\O
in Tic Tac Toe), we just add that as a parameter

(defun has-diagonal-3x3 (matrix x)
(if-match @(or ((@x . @nil)
(@nil @x . @nil)
(@nil @nil @x))
((@nil @nil @x)
(@nil @x . @nil)
(@x . @nil)))
matrix
t))

The pattern matcher will substitute the value of the existing
lexical x for the pattern variable @x.

2> (has-diagonal-3x3 '((x o o)
(o x _)
(_ _ x)) 'x)
t 3> (has-diagonal-3x3 '((x o o)
(o _ _)
(_ _ x)) 'x)
nil
4> (has-diagonal-3x3 '((x o o)
(o o x)
(o x x)) 'x)
nil
5> (has-diagonal-3x3 '((x o o)
(o o x)
(o x x)) 'o)
t

Backquote notation can be used.

(defun has-diagonal-3x3 (matrix x)
(if-match @(or ^((,x . ,nil)
(,nil ,x . ,nil)
(,nil ,nil ,x))
^((,nil ,nil ,x)
(,nil ,x . ,nil)
(,x . ,nil)))
matrix
t))

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

1

rocksolid light 0.9.8
clearnet tor