You are looking at historical revision 37751 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 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 have to be handled in constructors by supplying an additional argument, a sentinel-option.

Additional sentinel-option arguments are not provided, if a sub-pseudolist is constructed, e.g. for filter, member, drop, take. In that case, the sentinel of the original pseudolist is used.

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: Sentinel-options come first, procedures second and pseudolists last. To conform to this convention, I've changed the syntax of pl-iterate and pl-for.

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 opt . args)

constructs a new pseudolist with (sentinel opt).

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

creates a pseudolist with (sentinel opt) 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 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 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 opt fn)
[procedure] (pl-map opt fn pl)

maps fn over the pseudolist pl, returning a new pseudolist with (sentinel opt).

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 opt pl)

reverses its pseudolist argument, changing the sentinel to (sentinel opt).

pl-append

[procedure] (pl-append opt pl . pls)

appends all argument pseudolists to a new one with (sentinel opt).

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 opt pl-tree)

transforms a nested pseudolist to a flat pseudolist with (sentinel opt).

pl-for

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

creates a new pseudolist with (sentinel opt) 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 (pseudo-) 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

datatype

Examples


(import pseudolists)

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

(pl-reverse (Some-sentinel #f) '(0 1 2 3 . 4)) ;-> '(3 2 1 0 . #f)
(pl-reverse (No-sentinel) '(0 1 2 3 . 4)) ;-> '(3 2 1 0)

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

(pl-for (Some-sentinel #f) (add1 x) (x '(0 1 2 3 . #t))) ; map
  ;-> '(1 2 3 4 . #f)
(pl-for (No-sentinel) (add1 x) (x '(0 1 2 3))) ; map
  ;-> '(1 2 3 4)
(pl-for (No-sentinel) x (x '(0 1 2 3 4 5) (odd? x))) ; filter
  ;-> '(1 3 5)
(pl-for (Some-sentinel #f) (* 10 n)
        (n '(0 1 2 3 4 5) (positive? n) (even? n)))
  ;-> '(20 40 . #f)
(pl-for (No-sentinel) (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-for (No-sentinel) (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 03, 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.1
some sentinel-option arguments added; syntax-change of pl-for and pl-iterate
1.0
initial import