1. Pseudolists
    1. Rationale
    2. Module pseudolists
      1. pseudolists
      2. pl-maker
      3. pl?
      4. pl-of?
      5. pl-null?
      6. pl-length
      7. pl-iterate
      8. pl-at
      9. pl-head
      10. pl-sentinel
      11. pl-drop
      12. pl-drop-while
      13. pl-take
      14. pl-take-while
      15. pl-map
      16. pl-index
      17. filter
      18. pl-reverse
      19. pl-append
      20. pl-memp
      21. pl-member
      22. pl-memq
      23. pl-memv
      24. pl-fold-right
      25. pl-fold-right0
      26. pl-fold-left
      27. pl-fold-left0
      28. pl-adjoin
      29. pl-remove-dups
      30. pl-flatten
      31. pl-for
    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 lists. 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, and are not touched in a map routine. Moreover, constructors must supply the sentinel as an additional argument.

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.

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-maker

[procedure] (pl-maker sentinel . args)

constructs a new pseudolist. Note the position of the sentineal argument.

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 sentinel k fn)
[procedure] (pl-iterate sentinel k fn init)

creates a pseudolist with sentinel 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 pl)
[procedure] (pl-drop n pl)

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

pl-drop-while

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

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

pl-take

[procedure] (pl-take n pl)
[procedure] (pl-take 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 pl)
[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.

pl-index

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

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

filter

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

returns the sublist of lst consisting of all items passing the ok? predicate.

pl-reverse

[procedure] (pl-reverse pl)

reverses its pseudolist argument.

pl-append

[procedure] (pl-append pl . pls)

appends all argument pseudolists.

pl-memp

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

returns the sub-pseudolist starting at the first item which passes the ok? test.

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.

pl-remove-dups

[procedure] (pl-remove-dups pl)

removes all duplicates of lst.

pl-flatten

[procedure] (pl-flatten tree)

transforms a nested pseudolist to a flat list including sentinels.

pl-for

[syntax] (pl-for ((var pl ok-xpr ...) (var1 pl1 ok-xpr1 ...) ...) item-xpr)

creates a new pseudolist 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 pseudolist. 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-maker #f 0 1 2 3) ;-> '(0 1 2 3 . #f)
(pl-maker '() 0 1 2 3) ;-> '(0 1 2 3)
(pl-iterate #f 5 add1 0) ;-> '(0 1 2 3 4 . #f)

(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) ;-> 1
(pl-drop 2 '(0 1 2 3 . #f)) ;-> '(2 3 . #f)
(pl-take-while negative? '(1 3 2 4 . #f)) ;-> #f

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

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

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

Last update

Mar 24, 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.0
initial import