You are looking at historical revision 38335 of this page. It may differ significantly from its current revision.

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.

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

[parameter] (pl-sentinel)

gets and sets the sentinel to be needed by constructors. Default is '(), so that the routines work for lists as well. Mostly used in constructors.

pl-check-sentinel?

[procedure] (pl-check-sentinel?)
[procedure] (pl-check-sentinel? pl)

checks if pl's sentinel is equal to (pl-sentinel)

pl-change-sentinel

[procedure] (pl-change-sentinel new-sentinel)
[procedure] (pl-change-sentinel new-sentinel pl)

changes pl's sentinel

pl

[procedure] (pl . args)

creates a pseudolist, with sentinel from (pl-sentinel).

pl-maker

[procedure] (pl-maker len)
[procedure] (pl-maker len fill)

creates a pseudolist of length len with sentinel from (pl-sentinel) and items fill, which is (pl-sentinel) if not given.

pl-iterate

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

creates a pseudolist with sentinel from (pl-sentinel), applying fn to init recursively k times.

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

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

returns the tail of pl with the sentinel kept removing all head items with index less than n.

pl-drop-while

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

returns the tail pseudolist of pl with sentinel kept 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 pseudolist of pl with sentinel kept 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 pseudolist of pl with sentinel kept consisting of items until the first doesn't pass the ok? test.

pl-map

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

maps fn over the pseudolists pls, returning a new pseudolist with sentinel from the first pl with minimal length, applying fn successively to the list of car items up to the first list containing a pl-null item. This is R7RS-logic contrary to R5RS.

pl-for-each

[procedure] (pl-for-each fn pl . pls)

applies fn to successive cars of pl . pls as long as none of them is pl-null? This is R7RS-logic contrary to R5RS.

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 two values, the sublist of pl consisting of all items passing or not passing the ok? predicate respectively. The sentinel is kept in both values.

pl-reverse

[procedure] (pl-reverse pl)

reverses its pseudolist argument, keeping the sentinel.

pl-append

[procedure] (pl-append pl . pls)

appends all argument pseudolists to a new pseudolist with sentinel from the last pseudolist argument.

pl-memp

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

returns the sublist starting at the first item which passes the ok? test. The sentinel is preserved. 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 pl. As always, the sentinel is kept.

pl-remove-dups

[procedure] (pl-remove-dups pl)

removes all duplicates of pl.

pl-flatten

[procedure] (pl-flatten pl-tree)

transforms a nested pseudolist to a flat pseudolist with sentinel taken from the depth zero pseudolist.

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. The sentinel of the result is that of the first pseudolist argument.

Dependencies

none

Examples


(import pseudolists)

((pl-of? symbol?) '(a b . c)) ;-> #t
(pl-iterate add1 5 #f 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-take 2 '(0 1 2 3 . #f)) ;-> '(0 1 . #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 '(0 1 2 3)) ;-> '(1 2 3 4)
(pl-map add1 #f) ;-> #f

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

(pl-append '(0 1 . #t) '(2 3 . #t) '(4 5 . #t)) ;-> '(0 1 2 3 4 5 . #t)
(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 '(1 (2 3) . #t)) ;-> '(1 2 3 . #t)
(pl-flatten '(1 (2 (3 . #f) . #t) . #f)) ;-> '(1 2 3 . #f)
(pl-flatten #f) ;-> #f

(pl-collect (add1 x) (x '(0 1 2 3 . #t))) ; map
  ;-> '(1 2 3 4 . #t)
(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) . #f)

Last update

Mar 24, 2020

Author

Juergen Lorenz

License

Copyright (c) 2020, 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

2.0
sentinels kept, not stripped
1.4
pl-maker added
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