1. Pseudolists
    1. Rationale
    2. Module pseudolists
      1. pseudolists
      2. pl?
      3. pl-of?
      4. pl-null?
      5. pl-length
      6. pl-iterate
      7. pl-at
      8. pl-head
      9. pl-sentinel
      10. pl-drop
      11. pl-drop-while
      12. pl-take
      13. pl-take-while
      14. pl-map
      15. pl-index
      16. pl-filter
      17. pl-reverse
      18. pl-append
      19. pl-memp
      20. pl-member
      21. pl-memq
      22. pl-memv
      23. pl-fold-right
      24. pl-fold-right0
      25. pl-fold-left
      26. pl-fold-left0
      27. pl-adjoin
      28. pl-remove-dups
      29. pl-flatten
      30. pl-collect
    3. Dependencies
    4. Examples
  2. Last update
  3. Author
  4. License
  5. Version History

Pseudolists

This module exports routines to handle pseudolists as a generalisation of ordinary lists.

Rationale

Pseudolists differ from lists insofor, as their sentinels might be arbitrary atoms instead of the empty list. In other words, a pseudolist is either a pair or an atom. But since an atom is simply not a pair, everything is a pseudolist, in particular, a list is one.

So the routines of this module have to be written so that they work for lists as well. For example, this means, that the sentinel doesn't count in a length routine.

For simplicity, we have arrenged things in such a way, that routines, which create a pseudolist, in fact create a proper list. If you want an atom instead of the empty list as sentinel, simply append this atom to the routine's result.

Most functions in this module are written in curried and uncurried form. So the curried version can be used in maps and friends. The signature of both versions is documented below, but only the uncurried version is described. If the pseudolist argument is missing in the signature, it's the curried version.

Note, that the order of arguments is consistent: Procedures come first and pseudolists last.

Module pseudolists

pseudolists

[procedure] (pseudolists)
[procedure] (pseudolists sym)

documentation procedure. The first call shows the list of exported routines, the second documentation of the routine with symbol sym.

pl?

[procedure] (pl? xpr)

type predicate. Note that everything is a pseudolist!

pl-of?

[procedure] (pl-of? . preds)

returns a unary predicate, which tests, if its argument is passed by each predicate in preds.

pl-null?

[procedure] (pl-null? xpr)

not a pair.

pl-length

[procedure] (pl-length pl)

length of a pseudolist. The sentinel is not counted.

pl-iterate

[procedure] (pl-iterate fn k)
[procedure] (pl-iterate fn k init)

creates a list applying fn to init recursively k times.

pl-at

[procedure] (pl-at k)
[procedure] (pl-at k pl)

returns the kth item of pl.

pl-head

[procedure] (pl-head pl)

returns the list of items with pl's sentinel stripped.

pl-sentinel

[procedure] (pl-sentinel pl)

returns the sentinel of pl.

pl-drop

[procedure] (pl-drop n)
[procedure] (pl-drop n pl)

returns the tail of pl with the sentinel stripped removing all head items that pass the ok? test.

pl-drop-while

[procedure] (pl-drop-while ok?)
[procedure] (pl-drop-while ok? pl)

returns the tail of pl with the sentinel stripped starting with the first item that does not pass the ok? test.

pl-take

[procedure] (pl-take n)
[procedure] (pl-take n pl)

returns the head of pl up to but excluding index n, where n is less than or equal to pl's pl-length.

pl-take-while

[procedure] (pl-take-while ok?)
[procedure] (pl-take-while ok? pl)

returns the head of pl consisting of items which pass the ok? test.

pl-map

[procedure] (pl-map fn)
[procedure] (pl-map fn pl)

maps fn over the pseudolist pl, returning a new list.

pl-index

[procedure] (pl-index ok?)
[procedure] (pl-index ok? pl)

returns the index of the first item passing the ok? test, -1 otherwise.

pl-filter

[procedure] (pl-filter ok?)
[procedure] (pl-filter ok? pl)

returns the sublist of lst consisting of all items passing the ok? predicate. As always, the sentinel is stripped.

pl-reverse

[procedure] (pl-reverse pl)

reverses its pseudolist argument, stripping the sentinel.

pl-append

[procedure] (pl-append pl . pls)

appends all argument pseudolists to a new list.

pl-memp

[procedure] (pl-memp ok?)
[procedure] (pl-memp ok? pl)

returns the sublist starting at the first item which passes the ok? test. If no item passes the ok? test, #f is returned.

pl-member

[procedure] (pl-member x)
[procedure] (pl-member x pl)

same as (pl-memp (cut equal? <> x) pl).

pl-memq

[procedure] (pl-memq x)
[procedure] (pl-memq x pl)

same as (pl-memp (cut eq? <> x) pl).

pl-memv

[procedure] (pl-memv x)
[procedure] (pl-memv x pl)

same as (pl-memp (cut eqv? <> x) pl).

pl-fold-right

[procedure] (pl-fold-right op init)
[procedure] (pl-fold-right op init pl)

folds pl from the right with binary operation op and starting value init.

pl-fold-right0

[procedure] (pl-fold-right0 op)
[procedure] (pl-fold-right0 op pl)

folds (cdr pl) from the right with binary operation op and starting value (car pl).

pl-fold-left

[procedure] (pl-fold-left op init)
[procedure] (pl-fold-left op init pl)

folds pl from the left with binary operation op and starting value init.

pl-fold-left0

[procedure] (pl-fold-left0 op)
[procedure] (pl-fold-left0 op pl)

folds (cdr pl) from the left with binary operation op and starting value (car pl).

pl-adjoin

[procedure] (adjoin obj)
[procedure] (adjoin obj pl)

adds obj to the pseudolist pl, provided obj is not an item of lst. As always, the sentinel is stripped.

pl-remove-dups

[procedure] (pl-remove-dups pl)

removes all duplicates of lst.

pl-flatten

[procedure] (pl-flatten pl-tree)

transforms a nested pseudolist to a flat list.

pl-collect

[syntax] (pl-collect item-xpr (var pl ok-xpr ...) ....)

creates a new list by binding var to each element of the pseudolist pl in sequence, and if it passes the checks, ok-xpr ..., inserts the value of item-xpr into the resulting list. The qualifieres, (var pl ok-xpr ...), are processed sequentially from left to right, so that filters of a qualifier have access to the variables of qualifiers to its left.

Dependencies

none

Examples


(import pseudolists)

((pl-of? symbol?) '(a b . c)) ;-> #t
(pl-iterate add1 5 0) ;-> '(0 1 2 3 4)

(pl-length '(0 1 2 3 . 4)) ;-> 4
(pl-head '(0 . 1)) ;-> '(0)
(pl-at 2 '(0 1 2 3 . #f)) ;-> 2
(condition-case (pl-at 0 1)
  ((exn) #f)) ;-> #f

(pl-index odd? '(0 2 4 . 1)) ;-> -1
(pl-drop 0 1) ;-> '()
(pl-drop 2 '(0 1 2 3 . #f)) ;-> '(2 3)
(pl-take-while negative? '(1 3 2 4 . #f)) ;-> '()
(pl-take 2 '(0 1 2 3 . #f)) ;-> '(0 1))

(pl-filter odd? '(0 1 2 3 . 4)) ;-> '(1 3)
(pl-map add1 '(0 1 2 3 . 4)) ;-> '(1 2 3 4)
(pl-map add1 '(0 1 2 3 . 4)) ;-> '(1 2 3 4)
(pl-map add1 '(0 1 2 3)) ;-> '(1 2 3 4)
(pl-map add1 #f) ;-> '()

(pl-reverse '(0 1 2 3 . 4)) ;-> '(3 2 1 0)

(pl-append '(0 1 . #t) '(2 3 . #t) '(4 5 . #t)) ;-> '(0 1 2 3 4 5)
(pl-append '(0 1) '(2 3) #f) ;-> '(0 1 2 3)
(pl-adjoin 2 '(0 1 2 3 . #f)) ;-> '(0 1 2 3)
(pl-adjoin 1 '()) ;-> '(1)
(pl-remove-dups '(0 1 2 3 2 . 2)) ;-> '(0 1 3 2)

(pl-flatten '(1 (2 3) . #t)) ;-> '(1 2 3)
(pl-flatten '(1 (2 (3 . #f) . #t) . #f)) ;-> '(1 2 3)
(pl-flatten #f) ;-> '()

(pl-collect (add1 x) (x '(0 1 2 3 . #t))) ; map
  ;-> '(1 2 3 4)
(pl-collect (add1 x) (x '(0 1 2 3))) ; map
  ;-> '(1 2 3 4)
(pl-collect x (x '(0 1 2 3 4 5) (odd? x))) ; filter
  ;-> '(1 3 5)
(pl-collect (* 10 n)
            (n '(0 1 2 3 4 5) (positive? n) (even? n)))
  ;-> '(20 40)
(pl-collect (list c k)
            (c '(A B C)) (k '(1 2 3 4)))
  ;-> '((A 1) (A 2) (A 3) (A 4)
  ;     (B 1) (B 2) (B 3) (B 4)
  ;     (C 1) (C 2) (C 3) (C 4))
(pl-collect (list c k)
            (c '(A B C . #f)) (k '(1 2 3 4 . #t)))
  ;-> '((A 1) (A 2) (A 3) (A 4)
  ;     (B 1) (B 2) (B 3) (B 4)
  ;     (C 1) (C 2) (C 3) (C 4))

Last update

Jul 08, 2019

Author

Juergen Lorenz

License

Copyright (c) 2019, Juergen Lorenz
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the author nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission. 
  
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Version History

1.3
sentinel-options removed, all constructors create ordinary lists
1.2
macro pl-for renamed pl-collect
1.1
some sentinel-option arguments added; syntax-change of pl-for and pl-iterate
1.0
initial import