Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.54 KB | None | 0 0
  1. (define (accumulate start end null-value pred? term comb)
  2.   (if (> start end)
  3.       null-value
  4.       (comb
  5.        (if (pred? start)
  6.            (term start)
  7.            null-value)
  8.        (accumulate (+ start 1) end null-value pred? term comb)
  9.       )
  10.   )
  11. )
  12. ;(accumulate 1 100 0 (lambda (x) #t) (lambda (x) x) +)
  13.  
  14. (define (concat start end)
  15.   (define (num-digits n sum)
  16.   (if (< n 10)
  17.       (+ sum 1)
  18.       (num-digits (/ n 10) (+ sum 1))))
  19.   (define (concat-two k1 k2)
  20.     (if (= k2 0)
  21.         k1
  22.         (+ (* k1 (expt 10 (num-digits k2 0))) k2)))
  23.   (accumulate start end 0 (lambda (x) #t) (lambda (x) x) concat-two))
  24. ;(concat 1 9)
  25. ;(concat 19 23)
  26.  
  27.  
  28. (define (mix l1 l2)
  29.   (define (construct-mix-list l1 l2)
  30.   (if (or (null? l1) (null? l2))
  31.       ()
  32.       (cons (car l1) (construct-mix-list (cdr l2) (cdr l1)))))
  33.   (cons (construct-mix-list l1 l2) (construct-mix-list l2 l1)))
  34. ;(mix '(1 2 3 4 6 7) '(9 9 9 9 9 9))
  35. ;(mix '(9 9 9 9 9 9) '(1 2 3 4 6 7))
  36. ;(mix '(1 2 3 4 6 7) '(9 9 9 9 9))
  37. ;(mix '(9 9 9 9 9) '(1 2 3 4 6 7))
  38.  
  39. (define (repeat-k f k)
  40.   (if (= k 0)
  41.       (lambda (x) x)
  42.       (lambda (x) (f ((repeat-k f (- k 1)) x)))))
  43.  
  44. (define (task f)
  45.   (define (find-depth f a b k)
  46.     (define rep (repeat-k f k))
  47.     (if (> (rep a) b)
  48.         rep
  49.         (find-depth f a b (+ k 1))))
  50.   (lambda (a b)
  51.     (find-depth f a b 1)))
  52. ;(((task (lambda (x) (+ x 1))) 1 10) 2)
  53. ;(((task (lambda (x) (+ x 1))) 8 10) 2)
  54.  
  55. (define (switchsum f g n)
  56.   (define (nest f g n)
  57.     (cond
  58.       ((= n 0) (lambda (x) x))
  59.       ((= (remainder n 2) 0) (lambda (x) (g ((nest f g (- n 1)) x))))
  60.       (else (lambda (x) (f ((nest f g (- n 1)) x))))))
  61.   (if (= n 0)
  62.       (lambda (x) 0)
  63.       (lambda (x) (+ ((nest f g n) x) ((switchsum f g (- n 1)) x)))))
  64. ;((switchsum (lambda (x) (+ x 1)) (lambda (x) (* x 2)) 3) 5)
  65.  
  66. (define (ksum k a b)
  67.   (define (k-member n k digit sum)
  68.     (cond
  69.       ((< n 1) sum)
  70.       ((= (remainder digit k) 0) (k-member (quotient n 10) k (+ digit 1) (+ (* sum 10) (remainder n 10))))
  71.       (else (k-member (quotient n 10) k (+ digit 1) sum))))
  72.   (accumulate a b 0 (lambda (x) #t) (lambda (x) (k-member x k 0 0)) +))
  73. ;(ksum 1 1 200)
  74.  
  75. (define (replace l change)
  76.   (define (find-di ci cur-chg)
  77.     (cond
  78.       ((null? cur-chg) ci)
  79.       ((= ci (caar cur-chg)) (cdar cur-chg))
  80.       (else (find-di ci (cdr cur-chg)))))
  81.   (if (or (null? l) (null? change))
  82.       ()
  83.       (cons (find-di (car l) change) (replace (cdr l) (cdr change)))))
  84. ;(replace '(1 2 7 9 3) (list (cons 1 11) (cons 2 22) (cons 8 3) (cons 9 99) (cons 3 7)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement