From 3c73fd46dd0c487a568911b73346dc29dbbd9a74 Mon Sep 17 00:00:00 2001 From: "Edward L. Platt" Date: Wed, 24 Aug 2022 21:38:03 -0400 Subject: [PATCH] Move utility functions to separate module. --- deliberate/utils.scm | 74 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 deliberate/utils.scm diff --git a/deliberate/utils.scm b/deliberate/utils.scm new file mode 100644 index 0000000..0171045 --- /dev/null +++ b/deliberate/utils.scm @@ -0,0 +1,74 @@ +(define-module (deliberate utils) + #:use-module (srfi srfi-1) + #:export (const-list range reorder string-count zip))o + +(define (const-list length value) + (if (= length 0) + '() + (cons value (const-list (- length 1) value)))) + +;; Return a list of integers in the half-open interval [low, high) +(define (range low high) + (if (eq? low high) + '() + (cons low (range (+ 1 low) high)))) + +;; Combine lists component-wise +;; Similar to python zip() +;; +;; Parameters: +;; args: Any number of lists +;; +;; Returns: +;; A list. The nth element is a list of the +;; nth elements of each argument +;; +(define (zip . args) + (if (or (nil? args) (memq '() args)) + '() + (cons (map car args) + (apply zip (map cdr args))))) + +;; Reorder one list by another +;; A reordering is applied to lst such that the same reordering would +;; Put rank in asending oder. +;; +;; Parameters: +;; lst: The list to reorder +;; ranks: The list to sort by +;; +;; Returns: +;; A reordered copy of lst +;; +(define (reorder lst ranks) + (let* ((pairs (zip lst ranks)) + (ranked-pairs + (filter (lambda (pair) (not (eq? #f (cadr pair)))) + pairs)) + (sorted-pairs + (sort ranked-pairs (lambda (a b) (< (cadr a) (cadr b)))))) + (map car sorted-pairs))) + + +;; Same as string-count but assumes parameter is sorted +(define (string-count-sorted sorted) + (if (nil? sorted) + '() + (let* ((elt (car sorted)) + (total-length (length sorted)) + (rest (find-tail (lambda (x) (not (equal? x elt))) sorted)) + (rest-length (if rest (length rest) 0))) + (cons (cons elt (- total-length rest-length)) + (string-count-sorted rest))))) + +;; Counts the number of times each string appears in a list +;; +;; Parameters: +;; values: The list +;; +;; Returns: +;; An alist of the form ((string1 . count1) ...) +;; +(define (string-count values) + (let ((sorted (sort values string<))) + (string-count-sorted sorted)))