Paste number 7359: occurN

Index of paste annotations: 2 | 1

Paste number 7359: occurN
Pasted by: r2q2
When:4 years, 10 months ago
Share:Tweet this! | http://paste.lisp.org/+5OF
Channel:#scheme
Paste contents:
Raw Source | XML | Display As
;;;This function recurs down a list of markers and a list. If any of the elements of the list of markers occurs in the list the number that the function returns is increased by one.
(define occurN
  (lambda (markers l)
    (cond
     ((or (null? markers) (null? l)) 0)
     ((atom? (car markers))
      (cond
       ((eq? (car markers) (car l))
        (+ 1 (occurN markers (cdr l))
           (occurN (cdr markers) (cdr l))))
      (else (occurN (cdr markers) (cdr l))))))

(occurN '(bananas kiwis) '((curry '() (chicken) '())))
;;;This works fine
(occurN '(bananas kiwis) '(peaches apples bananas))
;;;This returns 0 which it should return 1

Annotations for this paste:

Annotation number 2: meep
Pasted by: evoli
When:4 years, 10 months ago
Share:Tweet this! | http://paste.lisp.org/+5OF/2
Paste contents:
Raw Source | Display As

(define (occurN markers l)
    (define (helper marker lst)
      (cond ((null? lst) 0)
	    ((eq? marker (car lst)) (+ 1 (helper marker (cdr lst))))
	    (else (helper marker (cdr lst)))))
    (if (null? markers) 0
	(+ (helper (car markers) l) (occurN (cdr markers) l))))

Annotation number 1: cleaned up and changed
Pasted by: swalters
When:4 years, 10 months ago
Share:Tweet this! | http://paste.lisp.org/+5OF/1
Paste contents:
Raw Source | Display As
(define occurN
  (lambda (markers l)
    (cond
     ((or (null? markers)
          (null? l))
      0)
     ((not (pair? (car markers)))
      (cond
       ((eq? (car markers) (car l))
        (+ 1 (occurN markers (cdr l))
           (occurN (cdr markers) (cdr l))))
       
       (else
        (+ (occurN markers (cdr l))
           (occurN (cdr markers) l))))))))

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

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