chickadee » pseudolists

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

pseudolistsprocedure
pseudolists symprocedure

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

pl-sentinel

pl-sentinelparameter

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?

pl-check-sentinel?procedure
pl-check-sentinel? plprocedure

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

pl-change-sentinel

pl-change-sentinel new-sentinelprocedure
pl-change-sentinel new-sentinel plprocedure

changes pl's sentinel

pl

pl #!rest argsprocedure

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

pl-maker

pl-maker lenprocedure
pl-maker len fillprocedure

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

pl-iterate

pl-iterate fn kprocedure
pl-iterate fn k initprocedure

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

pl?

pl? xprprocedure

type predicate. Note that everything is a pseudolist!

pl-of?

pl-of? #!rest predsprocedure

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

pl-null?

pl-null? xprprocedure

not a pair.

pl-length

pl-length plprocedure

length of a pseudolist. The sentinel is not counted.

pl-at

pl-at kprocedure
pl-at k plprocedure

returns the kth item of pl.

pl-head

pl-head plprocedure

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

pl-drop

pl-drop nprocedure
pl-drop n plprocedure

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

pl-drop-while

pl-drop-while ok?procedure
pl-drop-while ok? plprocedure

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

pl-take

pl-take nprocedure
pl-take n plprocedure

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

pl-take-while ok?procedure
pl-take-while ok? plprocedure

returns the head pseudolist of pl with sentinel kept consisting of items until the first doesn't pass the ok? test.

pl-map

pl-map fnprocedure
pl-map fn #!rest plsprocedure

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

pl-for-each fn pl #!rest plsprocedure

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

pl-index ok?procedure
pl-index ok? plprocedure

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

pl-filter

pl-filter ok?procedure
pl-filter ok? plprocedure

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

pl-reverse plprocedure

reverses its pseudolist argument, keeping the sentinel.

pl-append

pl-append pl #!rest plsprocedure

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

pl-memp

pl-memp ok?procedure
pl-memp ok? plprocedure

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

pl-member xprocedure
pl-member x plprocedure

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

pl-memq

pl-memq xprocedure
pl-memq x plprocedure

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

pl-memv

pl-memv xprocedure
pl-memv x plprocedure

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

pl-fold-right

pl-fold-right op initprocedure
pl-fold-right op init plprocedure

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

pl-fold-right0

pl-fold-right0 opprocedure
pl-fold-right0 op plprocedure

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

pl-fold-left

pl-fold-left op initprocedure
pl-fold-left op init plprocedure

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

pl-fold-left0

pl-fold-left0 opprocedure
pl-fold-left0 op plprocedure

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

pl-adjoin

adjoin objprocedure
adjoin obj plprocedure

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

pl-remove-dups

pl-remove-dups plprocedure

removes all duplicates of pl.

pl-flatten

pl-flatten pl-treeprocedure

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

pl-collect

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

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

Contents »