Paste number 82984: ACL Exercise 5.6

Index of paste annotations: 1 | 2 | 3

Paste number 82984: ACL Exercise 5.6
Pasted by: ???
When:2 years, 7 months ago
Share:Tweet this! | http://paste.lisp.org/+1S14
Channel:None
Paste contents:
Raw Source | XML | Display As
;;; iterative version

(defun intersperse (obj lst)
  (let ((result nil))
    (dolist (x lst (reverse (cdr result)))
      (setf result (nconc (list obj x) result)))))

;;;  recursive version

(defun intersperse (obj lst)
  (let ((res (intersperse-internal obj lst)))
    (subseq res 0 (- (length res) 1))))

(defun intersperse-internal (obj lst)
  (unless (null lst)
    (append (list (car lst) obj)
	    (intersperse-internal obj (cdr lst)))))

(defun makelist (x)
  (if (listp x)
      x
      (list x)))

;;; mapping version
(defun intersperse (obj lst)
  (reduce #'(lambda (x y) (append (makelist x) (makelist obj) (makelist y))) lst))
  

Annotations for this paste:

Annotation number 1: untitled
Pasted by: ???
When:2 years, 7 months ago
Share:Tweet this! | http://paste.lisp.org/+1S14/1
Paste contents:
Raw Source | Display As
;;; ACL 6.5 Example

(let ((counter 0))
  (defun reset-stamp ()
    (setf counter 0))
  (defun stamp ()
    (setf counter (+ counter 1))))

Annotation number 2: untitled
Pasted by: ???
When:2 years, 7 months ago
Share:Tweet this! | http://paste.lisp.org/+1S14/2
Paste contents:
Raw Source | Display As
public class Counter
{
	private static m_Counter;
	public static int Stamp()
	{
		return ++m_Counter;
	}
	
	public static int ResetStamp()
	{
		return (m_Counter=0);
	}	
}

Counter.Stamp();
Counter.Stamp();
Counter.ResetStamp();
Counter.Stamp();

Annotation number 3: ACL Exercise 6.4
Pasted by: ???
When:2 years, 7 months ago
Share:Tweet this! | http://paste.lisp.org/+1S14/3
Paste contents:
Raw Source | Display As
;;; My solution 

;; fancy-most returns `count' (default 2) functions with the highest score
;; fn - test function which gives a score

(defun fancy-most (fn lst &key (count 2))
  (let ((scores (mapcar #'(lambda (x) (cons x (funcall fn x))) lst)))
    (values-list (mapcar #'car (subseq (sort scores #'> :key #'cdr) 0 count)))))
    
;; test 
(fancy-most #'length '((a b) (a b c) (a))) ; => (A B C), (A B) 	      


;; Someone else's solution, possibly more efficient?

(defun most (fn lst)
  (if (null lst)
      (values nil nil)
      (if (= (length lst) 1)
        (values (car lst) nil)
        (let* ((wins (if (> (funcall fn (car lst)) (funcall fn (cadr lst)))
                         (car lst)
                         (cadr lst)))
               (wins2 (if (> (funcall fn (car lst)) (funcall fn (cadr lst)))
                         (cadr lst)
                         (car lst))))
          (dolist (obj (cdr (cdr lst)))
            (let ((score (funcall fn obj)))
              (if (> score (funcall fn wins))
                (setf wins2 wins
                      wins  obj)
                (if (> score (funcall fn wins2))
                  (setf wins2 obj)))))
          (values wins wins2)))))

(most #'length '((a b) (a b c) (a))) ; => (A B C), (A B)

Colorize as:
Show Line Numbers
Index of paste annotations: 1 | 2 | 3

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.