destructuring with 'let'

64-bit PicoLisp received a new feature (with version 3.1.9.2):

A destructuring 'let'.

That means that in addition to the normal 'let' calls with a single symbol/value pair

   (let A 1
      .. )

or a list of symbol/value pairs

   (let (A 1  B 2  C 3)
      .. )

pil64 now also supports nested structures of symbols:

   (let ((A . Z) (1 2 3 4))
      (list A Z) )
   -> (1 (2 3 4))

   (let (X 3  ((A . B) (C D) E . F) '((1 2 3) (4 5 6) 7 8 9)  Y 4)
      (list X A B C D E F Y) )
   -> (3 1 (2 3) 4 5 7 (8 9) 4)

   (let (((A . B) (C) . D) '((1 2 3) (4 5 6) 7 8 9))
      (list A B C D) )
   -> (1 (2 3) 4 (7 8 9))



You could achieve a similar goal with 'use' and 'match'

   (use (@A @B @C @D)
      (and
         (match '((@A . @B) (@C . @) . @D) '((1 2 3) (4 5 6) 7 8 9))
         (list (car @A) @B (car @C) @D) ) )
   -> (1 (2 3) 4 (7 8 9))

but that's quite tedious and ugly.

You can use 'NIL' as a placeholder to ignore parts of a pattern:

   (let (((A . NIL) NIL NIL D) '((1 2 3) (4 5 6) 7 8 9))
      (trail T) )
   -> (A 1 D 8)

'NIL' is not bound to the matching values and thus saves stack space.

https://picolisp.com/wiki/?destructuringlet

19mar16    erik
Revision History