• Q: Member function with list of lists

    From Brian McGuinness@21:1/5 to All on Wed Dec 14 10:12:11 2022
    I am experimenting with implementing algorithms from the Red Dragon Book using Clisp 2.49.92. I find that MEMBER doesn't work as I expected when applied to a list of lists. My list is:

    ; Each production starts with a nonterminal followed by the right side of the production.
    ; e.g. A -> B + C becomes '(A B + C)

    (SETQ GRAMMAR-4-11
    '(
    (E T EP)
    (EP + T EP)
    (EP epsilon)
    (T F TP)
    (TP * F TP)
    (TP epsilon)
    (F |(| E |)|)
    (F id)
    )
    )

    Now if I try (MEMBER '(EP epsilon) GRAMMAR-4-11) I get NIL, whereas I was expecting the third entry to be matched. But

    (EQUAL '(EP epsilon) (CADDR GRAMMAR-4-11))

    returns T as expected. And if I define

    (DEFUN MEMBER-OF (ELEMENT L)
    (COND ((NULL L) NIL)
    ((EQUAL ELEMENT (CAR L)) L)
    (T (MEMBER-OF ELEMENT (CDR L)))
    )
    )

    (DEFUN MEMBERP (ELEMENT L)
    (COND ((NULL L) NIL)
    ((EQUAL ELEMENT (CAR L)) T)
    (T (MEMBERP ELEMENT (CDR L)))
    )
    )

    these functions find the desired element successfully. So I don't understand why MEMBER can't find the element.

    --- Brian McGuinness

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lieven Marchand@21:1/5 to Brian McGuinness on Wed Dec 14 19:38:48 2022
    Brian McGuinness <b.mcguinness747@gmail.com> writes:

    Now if I try (MEMBER '(EP epsilon) GRAMMAR-4-11) I get NIL, whereas I was expecting the third entry to be matched. But

    (EQUAL '(EP epsilon) (CADDR GRAMMAR-4-11))

    returns T as expected.

    MEMBER uses EQL as test function when none is specified. (MEMBER '(EP
    epsilon) GRAMMAR-4-11 :test #'equal) should do what you expect.

    --
    Laat hulle almal sterf. Ek is tevrede om die wêreld te sien brand en die vallende
    konings te spot. Ek en my aasdier sal loop op die as van die verwoeste aarde.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jens Kallup@21:1/5 to All on Thu Dec 15 12:12:30 2022
    Am 14.12.2022 um 19:12 schrieb Brian McGuinness:

    what does:

    (F |(| E |)|)

    ??

    --
    Diese E-Mail wurde von Avast-Antivirussoftware auf Viren geprüft. www.avast.com

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Jens Kallup on Thu Dec 15 12:08:24 2022
    Jens Kallup <kallup-dev@web.de> writes:

    Am 14.12.2022 um 19:12 schrieb Brian McGuinness:

    what does:

    (F |(| E |)|)

    ??

    |xxx| is a way to write an atomic symbol that contains special charcters
    like ( and ):

    (symbol-name '|(|)
    "("

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jens Kallup@21:1/5 to All on Thu Dec 15 13:25:11 2022
    E:\SBCL\tests>sbcl --script test1.lisp
    ((EP *EPSILON*) (T F TP) (TP * F TP) (TP *EPSILON*) (F ( E )) (F ID))

    but i like to expect: (EP *EPSILON*)
    How can I do this.

    Thanks, Jens

    ; code running with sbcl
    ; test1.lisp:
    ;
    (defvar *GRAMMAR-4-11*)
    (defvar *epsilon*)

    (SETQ *GRAMMAR-4-11*
    '(
    (E T EP)
    (EP + T EP)
    (EP *epsilon*)
    (T F TP)
    (TP * F TP)
    (TP *epsilon*)
    (F |(| E |)|)
    (F id)
    )
    )

    (princ (MEMBER '(EP *epsilon*) *GRAMMAR-4-11* :test #'equal))


    --
    Diese E-Mail wurde von Avast-Antivirussoftware auf Viren geprüft. www.avast.com

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jens Kallup@21:1/5 to All on Thu Dec 15 14:11:31 2022
    ok, I have it done.
    But I don't know, if it the right way:


    (print (car (member '(T F TP) *GRAMMAR-4-11* :test #'equal)))
    (print (car (member '(EP *epsilon*) *GRAMMAR-4-11* :test #'equal)))


    This will me display the member of first occurrence.

    Jens

    --
    Diese E-Mail wurde von Avast-Antivirussoftware auf Viren geprüft. www.avast.com

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Spiros Bousbouras@21:1/5 to Jens Kallup on Thu Dec 15 13:54:04 2022
    On Thu, 15 Dec 2022 14:11:31 +0100
    Jens Kallup <kallup-dev@web.de> wrote:
    ok, I have it done.
    But I don't know, if it the right way:


    (print (car (member '(T F TP) *GRAMMAR-4-11* :test #'equal)))
    (print (car (member '(EP *epsilon*) *GRAMMAR-4-11* :test #'equal)))


    This will me display the member of first occurrence.

    It's correct but you may find using FIND (no pun intended) more straightforward.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jens Kallup@21:1/5 to All on Thu Dec 15 16:22:05 2022
    Am 15.12.2022 um 14:54 schrieb Spiros Bousbouras:

    It's correct but you may find using FIND (no pun intended) more straightforward.

    in this context:

    https://dpaste.com/FQJWV9QMT

    how can i combine i1 with "ich", and h1 with "habe", so the german
    sentence part "ich habe" will be produced.

    Like the Input:
    (member '(i1 h1))

    give me:
    (ich habe)

    is that possible ?

    Thanks for reading
    Jens

    --
    Diese E-Mail wurde von Avast-Antivirussoftware auf Viren geprüft. www.avast.com

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jens Kallup@21:1/5 to All on Thu Dec 15 18:18:31 2022
    maybe this is better:

    (defvar *grammar*)
    (defvar *alist*)

    (defvar *i0*)
    (defvar *i1*)

    (setq *alist*
    '(
    (1 . ich)
    (2 . du)

    (10 . habe)
    (20 . dich)

    (30 . gefragt)
    )
    )

    (setq *grammar*
    '((1)
    (1 10)
    (1 10 20)
    (1 10 20 30)))

    (setq *i0* (assoc '1 *grammar*))

    ; here (1 . ich)
    ; but I would test: if *i0* (1) is in alist
    ; when true, then setq ich, else print error:
    ;
    (setq *i1* (assoc '1 *alist* ))

    (print *i0* ) ; (1)
    (print *i1* ) ; (1 . ICH)



    --
    Diese E-Mail wurde von Avast-Antivirussoftware auf Viren geprüft. www.avast.com

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Brian McGuinness@21:1/5 to All on Thu Dec 15 13:11:03 2022
    Ok, thanks. This makes more sense now.

    --- Brian McGuinness

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)