File: lists.lisp

package info (click to toggle)
araneida 0.90.1-dfsg-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 700 kB
  • ctags: 643
  • sloc: lisp: 4,878; perl: 166; sh: 109; makefile: 34
file content (42 lines) | stat: -rw-r--r-- 1,473 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(in-package :araneida)

;; (foldr (lambda (x y) (string-append x "; " y)) '(" 3" "2" "1"))
;; => 3; 2; 1
;; (foldr (lambda (x y) (string-append "(" x "; " y ")")) '(" 3" "2" "1"))
;; => ( 3; (2; 1))

(defun foldr (func list)
  "Fold FUNC over LIST, starting at the right-hand end"
  (cond ((null (cdr list))
         (car list))
	(t 
	 (funcall func (car list) (foldr func (cdr list) )))))

;; (foldl (lambda (x y) (string-append x "; " y)) '(" 3" "2" "1"))
;; => 3; 2; 1
;; (foldl (lambda (x y) (string-append "(" x "; " y ")")) '(" 3" "2" "1"))
;; => (( 3; 2); 1)

(defun foldl (f lst) (foldr (lambda (y x) (funcall f x y)) (reverse lst)))

(defun join (delimiter elements)
  "Returns a new string consisting of ELEMENTS with DELIMITERs between them.  Elements are converted to strings as if by PRINC-TO-STRING"
  (with-output-to-string (o)
    (loop for front on elements
	  if (> (length front) 1)
	  do (progn (princ (car front) o) (princ delimiter o))
	  else
	  do (princ (car front) o))))

(defun choose-best (list test &key (key 'identity))
  "Effectively equivalent to (elt (sort list test :key key) 0) but potentially faster"  
  (loop for el in list
        for so-far = el then (if (funcall test (funcall key el)
                                          (funcall key so-far))
                                 el
                               so-far)
        finally (return so-far)))

#|
(choose-best '(#\1 #\2 #\1 #\8 #\3 #\0) #'> :key #'char-code) => 8
|#