Learning Pilog — 5: Lists

What is a list in Pilog?

(John Vincent Jules  Yolanda)
(John (robber Honey_Bunny) @X 2 John)
( () (dead z) (2 (b c)) () @Z (2 (b c)))
  • list elements can be a mix of data structures, like numbers (2), variables (@X, complex terms ((dead @Z).
  • a list can have duplicated values.
  • an empty list is still a list.
  • lists can be nested: (2 (b c)).
: (? (equal (@X . @Y) (John Vincent Jules Yolanda)))
@X=John @Y=(Vincent Jules Yolanda)
-> NIL
: (? (equal (@X . @Y) (John Vincent)))
@X=John @Y=(Vincent)
-> NIL
: (? (equal (@X . @Y) ()))
-> NIL
: (? (equal (@X @Y . @W) (John Vincent Jules Yolanda)))
@X=John @Y=Vincent @W=(Jules Yolanda)
-> NIL
: (? (equal (@ @ @Z . @) (John Vincent Jules Yolanda)))
-> NIL

The member predicate

  • Query: Is john a member of the list?
: (? (member John (John Vincent Jules Yolanda)))
-> T
(be member (@X (@X . @)))
(be member (@X (@ . @Y)) (member @X @Y))
  1. The first line merely checks if the first list element is equal to @X.
  2. The second element splits the list in head and tail. We know that the head is not @X, otherwise we would have already received T after the first line. So we can try our luck with the tail: member @X @Y).

Example: A small Translator

(be tran (eins one))
(be tran (zwei two))
(be tran (drei three))
(be tran (vier four))
(be tran (fuenf five))
(be tran (sechs six))
(be tran (sieben seven))
(be tran (acht eight))
(be tran (neun nine))
(be tran (zehn ten))
(be listtran (NIL NIL))
(be listtran ((@Hg . @Tg) (@He . @Te))
(tran @Hg @He)
(be listtran (NIL NIL))(be listtran ((@Hg . @Tg) . (@He  . @Te))
(tran @Hg @He)
(listtran @Tg @Te) )
: (? (listtran (eins zwei drei) @X))
@X=(one two three)
-> NIL



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mia Temma

Mia Temma

These are cross-posts from my blog https://picolisp-explored.com. I’m writing about PicoLisp for beginners. Welcome!