Learning Pilog — 4: Recursion

A recursive example

(be is_digesting (@X @Y)
(just_ate @X @Y) )
(be is_digesting (@X @Y)
(just_ate @X @Z)
(is_digesting @Z @Y) )
(be just_ate (Mosquito (blood John)))
(be just_ate (Frog Mosquito ))
(be just_ate (Stork Frog))
:  (? (is_digesting Stork @X))
@X=Frog
@X=Mosquito
@X=(Blood John)
-> NIL

The order matters

  1. it has eaten @X or
  2. has eating something that has eaten @X.
(be is_digesting (@X @Y)
(just_ate @X @Z)
(is_digesting @Z @Y) )
(be is_digesting (@X @Y)
(just_ate @X @Y) )
:  (? (is_digesting Stork @X))
@X=(Blood John)
@X=Frog
@X=Mosquito
-> NIL

How NOT To Do It

(be is_digesting (@X @Y)
(is_digesting @Z @Y)
(just_ate @X @Z) )
(be is_digesting (@X @Y)
(just_ate @X @Y) )
: (? (is_digesting Stork @X))
@X=Frog
@X=Mosquito
@X=(blood John)
: (rules 'is_digesting 'just_ate)
1 (be is_digesting (@X @Y) (just_ate @X @Y))
2 (be is_digesting (@X @Y) (is_digesting @Z @Y) (just_ate @X @Z))
1 (be just_ate (Mosquito (blood John)))
2 (be just_ate (Frog Mosquito))
3 (be just_ate (Stork Frog))
: (? is_digesting just_ate (is_digesting Stork @X))
1 (is_digesting Stork @Y)
3 (just_ate Stork Frog)
@X=Frog
2 (is_digesting Stork @Y)
1 (is_digesting @X @Y)
1 (just_ate Mosquito (blood John))
2 (just_ate Frog Mosquito)
3 (just_ate Stork Frog)
@X=Mosquito
2 (is_digesting @X @Y)
1 (is_digesting @X @Y)
1 (just_ate Mosquito (blood John))
2 (just_ate Frog Mosquito)
3 (just_ate Stork Frog)
2 (just_ate Frog Mosquito)
3 (just_ate Stork Frog)
3 (just_ate Stork Frog)

Example: Travel plans

(be directTrain (Saarbruecken Dudweiler))
(be directTrain (Forbach Saarbruecken))
(be directTrain (Freyming Sorbach))
(be directTrain (StAvold Freyming))
(be directTrain (Fahlquemont StAvold))
(be directTrain (Metz Fahlquemont))
(be directTrain (Nancy Metz))
(be travelBetween (@X @Y)
(be travelBetween (@X @Y)
(directTrain @X @Z)
(travelBetween @Z @Y))
(be travelBetween (@X @Y)
(directTrain @X @Y))
(be travelBetween (@Y @X)
(directTrain @Y @X))
...

--

--

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!