Advertisement
VladNitu

matiPlayground

Apr 4th, 2023
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 5.03 KB | None | 0 0
  1. object Solution {
  2.  
  3.   /**
  4.     * Use these utility functions to solve the assignment.
  5.     */
  6.   def util =
  7.     """
  8.      (zip-with
  9.        (lambda (f xs ys)
  10.          (if (and (is-nil xs) (is-nil ys))
  11.            nil
  12.            (cons
  13.              (f (head xs) (head ys))
  14.              (zip-with f (tail xs) (tail ys))))))
  15.      (map
  16.        (lambda (f xs)
  17.          (if (is-nil xs) nil
  18.            (cons (f (head xs)) (map f (tail xs))))))
  19.      (diff
  20.        (lambda (xs ys)
  21.          (if (is-nil xs)
  22.            nil
  23.            (if (is-nil ys)
  24.              xs
  25.              (if (num< (head xs) (head ys))
  26.                (cons (head xs) (diff (tail xs) ys))
  27.                (if (num= (head xs) (head ys))
  28.                  (diff (tail xs) (tail ys))
  29.                  (diff xs (tail ys))))))))
  30.      (take
  31.        (lambda (n xs)
  32.          (if (num= n 0)
  33.            nil
  34.            (cons (head xs) (take (- n 1) (tail xs))))))
  35.    """
  36.  
  37.   /**
  38.     * Using these utilities, we can define a stream of natural numbers:
  39.     */
  40.   def nats =
  41.     """
  42.      (letrec ("""+util+"""
  43.               (nats (cons 0 (map (lambda (x) (+ x 1)) nats))))
  44.        nats)
  45.    """
  46.  
  47.   /**
  48.     * Write a program that returns a stream of even natural numbers.
  49.     */
  50.  
  51.   // helper
  52.   // def timesTwo = "(lambda (x) (* 2 x))"
  53.   // def evens =
  54.   //   s"""
  55.  //   (let ((timesTwo (lambda (x) (* 2 x))))
  56.  //   (map timesTwo nats)
  57.  //   )
  58.  //   """
  59.  
  60.   // def evens = """
  61.  //     (letrec ("""+util+"""
  62.  //              (evens (cons 0 (map (lambda (x) (* x 2)) evens))))
  63.  //       nats)
  64.  //   """
  65.  
  66.   def evens =
  67.   """
  68.      (letrec ("""+util+"""
  69.               (evens (cons 0 (map (lambda (x) (+ x 2)) evens))))
  70.        evens)
  71.    """
  72.   //  """
  73.  //     (letrec ("""+util+"""
  74.  //              (evensss (cons 0 (map (lambda (x) (* (+ x 1) 2)) evensss))))
  75.  //       evensss)
  76.  //   """
  77.  
  78.   /**
  79.     * Use the the `evens`, `nats`, and `diff` functions to define a stream of
  80.     * odd natural numbers.
  81.     */
  82.   def odds =
  83.     """
  84.      (letrec ("""+util+"""
  85.               (nats (cons 0 (map (lambda (x) (+ x 1)) nats)))
  86.               (evens (cons 0 (map (lambda (x) (+ x 2)) evens))))
  87.        (diff nats evens))
  88.    """
  89.  
  90.   /**
  91.     * Define a function `multiples` that takes a number `n` as input and returns
  92.     * a stream containing all multiples of `n`.
  93.     */
  94.   def multiples =
  95.  
  96.   """
  97.  (letrec ("""+util+"""
  98.               (nats (cons 1 (map (lambda (x) (+ x 1)) nats)))
  99.               (mults (lambda (n) (map (lambda (x) (* x n)) nats)))
  100.               )
  101.        mults)
  102.  """
  103.  
  104.  
  105. // Alternative:
  106.  
  107.   // """
  108.  //   (letrec ((zip-with
  109.  //       (lambda (f xs ys)
  110.  //         (if (and (is-nil xs) (is-nil ys))
  111.  //           nil
  112.  //           (cons
  113.  //             (f (head xs) (head ys))
  114.  //             (zip-with f (tail xs) (tail ys))))))
  115.  //     (map
  116.  //       (lambda (f xs)
  117.  //         (if (is-nil xs) nil
  118.  //           (cons (f (head xs)) (map f (tail xs))))))
  119.  //     (diff
  120.  //       (lambda (xs ys)
  121.  //         (if (is-nil xs)
  122.  //           nil
  123.  //           (if (is-nil ys)
  124.  //             xs
  125.  //             (if (num< (head xs) (head ys))
  126.  //               (cons (head xs) (diff (tail xs) ys))
  127.  //               (if (num= (head xs) (head ys))
  128.  //                 (diff (tail xs) (tail ys))
  129.  //                 (diff xs (tail ys))))))))
  130.  //     (take
  131.  //       (lambda (n xs)
  132.  //         (if (num= n 0)
  133.  //           nil
  134.  //           (cons (head xs) (take (- n 1) (tail xs))))))
  135.  //     (mult (lambda (n) (cons n (map (lambda (x) (+ x n)) (mult n))))))
  136.  
  137.  //   mult)
  138.  
  139.  // """
  140.  
  141.  
  142.  
  143.  
  144.  
  145.   /**
  146.     * Define a function that returns a stream of primes, using the sieve of Eratosthenes:
  147.     * https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
  148.     *
  149.     * Hint: you can use `nats`, `diff`, and `multiples`
  150.     */
  151.   def eratosthenes =
  152.  
  153.  
  154.     """
  155. (letrec (
  156.           (zip-with
  157.        (lambda (f xs ys)
  158.          (if (and (is-nil xs) (is-nil ys))
  159.            nil
  160.            (cons
  161.              (f (head xs) (head ys))
  162.              (zip-with f (tail xs) (tail ys))))))
  163.      (map
  164.        (lambda (f xs)
  165.          (if (is-nil xs) nil
  166.            (cons (f (head xs)) (map f (tail xs))))))
  167.      (diff
  168.        (lambda (xs ys)
  169.          (if (is-nil xs)
  170.            nil
  171.            (if (is-nil ys)
  172.              xs
  173.              (if (num< (head xs) (head ys))
  174.                (cons (head xs) (diff (tail xs) ys))
  175.                (if (num= (head xs) (head ys))
  176.                  (diff (tail xs) (tail ys))
  177.                  (diff xs (tail ys))))))))
  178.      (take
  179.        (lambda (n xs)
  180.          (if (num= n 0)
  181.            nil
  182.            (cons (head xs) (take (- n 1) (tail xs))))))
  183.      (nats (cons 1 (map (lambda (x) (+ x 1)) nats)))
  184.      (mults (lambda (n) (map (lambda (x) (* x n)) nats)))
  185.      (sieve (lambda (er) (cons (head er) (sieve (diff er (mults (head er)))))))
  186.      (primes (sieve (map (lambda (x) (+ x 1)) nats))))
  187.        primes)
  188.  """
  189.  
  190.  
  191.  
  192.  
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement