Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.41 KB | None | 0 0
  1. #|
  2.  
  3. CS 2800 Homework 2 - Spring 2017
  4.  
  5. This homework is done in groups. More elaborate instructions will be
  6. posted on the course website soon:
  7.  
  8. * One group member will create a group in BlackBoard.
  9.  
  10. * Other group members then join the group.
  11.  
  12. * Homework is submitted once. Therefore make sure the person
  13. submitting actually does so. In previous terms when everyone needed
  14. to submit we regularly had one person forget but the other submissions
  15. meant the team did not get a zero. Now if you forget, your team gets 0.
  16. - It wouldn't be a bad idea for groups to email confirmation messages
  17. to each other to reduce anxiety.
  18.  
  19. * Submit the homework file (this file) on Blackboard. Do not rename
  20. this file. There will be a 10 point penalty for this.
  21.  
  22. * You must list the names of ALL group members below, using the given
  23. format. This way we can confirm group membership with the BB groups.
  24. If you fail to follow these instructions, it costs us time and
  25. it will cost you points, so please read carefully.
  26.  
  27. The format should be: FirstName1 LastName1, FirstName2 LastName2, ...
  28. For example:
  29. Names of ALL group members: David Sprague, Peizun Liu
  30.  
  31. There will be a 10 pt penalty if your names do not follow this format.
  32. Names of ALL group members: John Loar, Akshat Dhankher
  33.  
  34. * Later in the term if you want to change groups, the person who created
  35. the group should stay in the group. Other people can leave and create
  36. other groups or change memberships (the Axel Rose membership clause).
  37. We will post additional instructions about this later.
  38.  
  39. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  40.  
  41. For this homework you will need to use ACL2s.
  42.  
  43. Technical instructions:
  44.  
  45. - open this file in ACL2s as hw02.lisp
  46.  
  47. - make sure you are in BEGINNER mode. This is essential! Note that you can
  48. only change the mode when the session is not running, so set the correct
  49. mode before starting the session.
  50.  
  51. - insert your solutions into this file where indicated (usually as "...")
  52.  
  53. - only add to the file. Do not remove or comment out anything pre-existing.
  54.  
  55. - make sure the entire file is accepted by ACL2s. In particular, there must
  56. be no "..." left in the code. If you don't finish all problems, comment
  57. the unfinished ones out. Comments should also be used for any English
  58. text that you may add. This file already contains many comments, so you
  59. can see what the syntax is.
  60.  
  61. - when done, save your file and submit it as hw02.lisp
  62.  
  63. - avoid submitting the session file (which shows your interaction with the
  64. theorem prover). This is not part of your solution. Only submit the lisp
  65. file.
  66.  
  67. Instructions for programming problems:
  68.  
  69. For each function definition, you must provide both contracts and a body.
  70.  
  71. You must also ALWAYS supply your own tests. This is in addition to the
  72. tests sometimes provided. Make sure you produce sufficiently many new test
  73. cases. This means: cover at least the possible scenarios according to the
  74. data definitions of the involved types. For example, a function taking two
  75. lists should have at least 4 tests: all combinations of each list being
  76. empty and non-empty.
  77.  
  78. Beyond that, the number of tests should reflect the difficulty of the
  79. function. For very simple ones, the above coverage of the data definition
  80. cases may be sufficient. For complex functions with numerical output, you
  81. want to test whether it produces the correct output on a reasonable
  82. number of inputs.
  83.  
  84. Use good judgment. For unreasonably few test cases we will deduct points.
  85.  
  86. We will use ACL2s' check= function for tests. This is a two-argument
  87. function that rejects two inputs that do not evaluate equal. You can think
  88. of check= roughly as defined like this:
  89.  
  90. (defunc check= (x y)
  91. :input-contract (equal x y)
  92. :output-contract (equal (check= x y) t)
  93. t)
  94.  
  95. That is, check= only accepts two inputs with equal value. For such inputs, t
  96. (or "pass") is returned. For other inputs, you get an error. If any check=
  97. test in your file does not pass, your file will be rejected.
  98.  
  99. |#
  100.  
  101. #|
  102.  
  103. Since this is our first programming exercise, we will simplify the
  104. interaction with ACL2s somewhat: instead of asking it to formally *prove*
  105. the various conditions for admitting a function, we will just require that
  106. they be *tested* on a reasonable number of inputs. This is achieved using
  107. the following directive (do not remove it!):
  108.  
  109. |#
  110.  
  111. :program
  112. #|
  113.  
  114. Notes:
  115.  
  116. 1. Testing is cheaper but less powerful than proving. So, by turning off
  117. proving and doing only testing, it is possible that the functions we are
  118. defining cause runtime errors even if called on valid inputs. In the future
  119. we will require functions complete with admission proofs, i.e. without the
  120. above directive. For this first homework, the functions are simple enough
  121. that there is a good chance ACL2s's testing will catch any contract or
  122. termination errors you may have.
  123.  
  124. 2. The tests ACL2s runs test only the conditions for admitting the
  125. function. They do not test for "functional correctness", i.e. does the
  126. function do what it is supposed to do? ACL2s has no way of telling what
  127. your function is supposed to do. That is what your own tests are for.
  128.  
  129. 3. For now testing is written using check= expressions. These take the following
  130. format (and should be familiar to those of you that took Fundies I)
  131. (check= <expression> <thing it should be equal to>)
  132. EX: (check= (- 4/3 1) 1/3)
  133.  
  134. |#
  135.  
  136. #|
  137. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  138. Part I:
  139. The functions below should warm you up for the rest of the
  140. assignment.
  141. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  142. |#
  143.  
  144. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  145. ;; Define
  146. ;; TriXOR: Bool x Bool x Bool -> Bool
  147. ;;
  148. ;; (TriXOR x y z) takes three booleans x y and z and returns true if and
  149. ;; only if exactly 1 of the variables is true. This is XOR applied
  150. ;; to three variables.
  151. (defunc TriXOR (x y z)
  152. :input-contract (and (booleanp x) (booleanp y) (booleanp z))
  153. :output-contract (booleanp (TriXOR x y z))
  154. (or (and x (not y) (not z))
  155. (and (not x) y (not z))
  156. (and (not x) (not y) z)))
  157.  
  158. (check= (TriXOR t t nil) nil)
  159. (check= (TriXOR nil t nil) t)
  160. ;; Additional checks needed
  161. (check= (TriXOR t t t) nil)
  162. (check= (TriXOR nil nil t) t)
  163. (check= (TriXOR nil nil nil) nil)
  164.  
  165.  
  166. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  167. ;; Define
  168. ;; how-many: All x List -> Nat
  169. ;;
  170. ;; (how-many e l) returns the number of occurrences of element e
  171. ;; in list l.
  172. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  173. (defunc how-many (e l)
  174. :input-contract (listp l)
  175. :output-contract (natp (how-many e l))
  176. (cond ((endp l) 0)
  177. ((equal e (first l)) (+ (how-many e (rest l)) 1))
  178. (t (how-many e (rest l)))))
  179.  
  180. (check= (how-many 1 ()) 0)
  181. (check= (how-many 1 '(1 1)) 2)
  182. ;; Add additional tests
  183. (check= (how-many 1 '(1 2 1)) 2)
  184. (check= (how-many 'x '(x y z)) 1)
  185. (check= (how-many 7 '(1 2 3)) 0)
  186.  
  187. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  188. ; Define
  189. ; same-multiplicity: List x List x List -> Boolean
  190. ;
  191. ; (same-multiplicity l l1 l2) returns t iff every element of l occurs in l1
  192. ; and l2 the same number of times.
  193. ; REMEMBER: you can ALWAYS use functions from earlier in the
  194. ; program. The functions were given in this order for a reason.
  195. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  196. (defunc same-multiplicity (l l1 l2)
  197. :input-contract (and (listp l) (listp l1) (listp l2))
  198. :output-contract (booleanp (same-multiplicity l l1 l2))
  199. (or (endp l)
  200. (and (equal (how-many (first l) l1)
  201. (how-many (first l) l2))
  202. (same-multiplicity (rest l) l1 l2))))
  203.  
  204. (check= (same-multiplicity '(1) '(2 1 3) '(1 2 2)) t)
  205. (check= (same-multiplicity '(1 2) '(2 1 3) '(1 2 2)) nil)
  206. (check= (same-multiplicity () '(2 1 3) '(1 2 2)) t)
  207. (check= (same-multiplicity '(1 2) () '(1 2)) nil)
  208. (check= (same-multiplicity '(1 2) '(1 2) ()) nil)
  209.  
  210. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  211. ;; flat-listp
  212. ;; flat-listp: Any -> Boolean
  213. ;;
  214. ;; (flat-listp l) takes an input l and returns true if and only if
  215. ;; l is a list AND each element in l is not a list.
  216. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  217. (defunc flat-listp (l)
  218. :input-contract t
  219. :output-contract (booleanp (flat-listp l))
  220. (and (listp l)
  221. (or (endp l)
  222. (and (atom (first l))
  223. (flat-listp (rest l))))))
  224.  
  225. (check= (flat-listp '(1 2 (3))) nil)
  226. (check= (flat-listp '(1 2 3)) t)
  227. ;; Make sure you add additional tests
  228. (check= (flat-listp ()) t)
  229. (check= (flat-listp '(1 (2) (3 (4)))) nil)
  230. (check= (flat-listp '(1 2 3 4 (5))) nil)
  231. (check= (flat-listp '(1 2 3 4 5)) t)
  232.  
  233. #|
  234. Part II: List manipulation
  235. This following section deals with functions involving lists in general.
  236. Some functions you write may be useful in subsequent functions.
  237. In all cases, you can define your own helper functions if that simplifies
  238. your coding
  239. |#
  240.  
  241. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  242. ;; merge-lists
  243. ;; merge-lists: List x List -> List
  244. ;;
  245. ;; (merge-lists l1 l2) takes two lists l1 and l2 and returns a
  246. ;; list with all elements from both alternating between elements in l1
  247. ;; and l2....consider it zipping the lists together.
  248. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  249. (defunc merge-lists (l1 l2)
  250. :input-contract (and (listp l1) (listp l2))
  251. :output-contract (listp (merge-lists l1 l2))
  252. (cond ((endp l1) l2)
  253. ((endp l2) l1)
  254. (t (cons (first l1) (cons (first l2)
  255. (merge-lists (rest l1)
  256. (rest l2)))))))
  257.  
  258. (check= (merge-lists '(a b c d) '(1 2 3 4 5))
  259. '(a 1 b 2 c 3 d 4 5))
  260. (check= (merge-lists () '(1 2 3)) '(1 2 3))
  261. (check= (merge-lists '(1 2 3) ()) '(1 2 3))
  262. (check= (merge-lists '(a b c) '(1 2 3 4 5)) '(a 1 b 2 c 3 4 5))
  263. (check= (merge-lists '(1 2 3 4 5) '(a b c)) '(1 a 2 b 3 c 4 5))
  264.  
  265. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  266. ;; remove-nil
  267. ;; remove-nil: List -> List
  268. ;;
  269. ;; (remove-nil) takes an input list l and creates a new list with
  270. ;; every element of nil removed
  271.  
  272. (defunc remove-nil (l)
  273. :input-contract (listp l)
  274. :output-contract (listp (remove-nil l))
  275. (cond ((endp l) l)
  276. ((equal nil (first l)) (remove-nil (rest l)))
  277. (t (cons (first l) (remove-nil (rest l))))))
  278.  
  279. (check= (remove-nil ()) ())
  280. (check= (remove-nil '(1 2 3)) '(1 2 3))
  281. (check= (remove-nil '(nil 2 3)) '(2 3))
  282. (check= (remove-nil '(nil nil nil)) ())
  283. (check= (remove-nil '(1 nil 3)) '(1 3))
  284.  
  285. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  286. ;; flatten-list
  287. ;; flatten-list: List -> List (a flat list)
  288. ;;
  289. ;; (flatten-list l) takes an input list l and creates a new list
  290. ;; with all the same atomic elements (excluding nil) in l but the new list
  291. ;; has no sub-lists.
  292. ;; HINT: this may be a complex function for many of you. Let's pretend
  293. ;; (first l) is a list. What happens if I append (first l) to (rest l)??
  294. ;; Can I call flatten-list again on this new list? Am I guaranteed
  295. ;; to stop recursing?
  296. ;; When can I NOT call app?
  297. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  298. (defunc flatten-list (l)
  299. :input-contract (listp l)
  300. :output-contract (flat-listp (flatten-list l))
  301. (cond ((flat-listp l) (remove-nil l))
  302. ((equal nil (first l)) (flatten-list (rest l)))
  303. ((atom (first l)) (cons (first l) (flatten-list (rest l))))
  304. (t (flatten-list (append (first l) (rest l))))))
  305.  
  306. (check= (flatten-list '((1 2 3) nil 4 (5 6))) '(1 2 3 4 5 6))
  307. ; Add additional tests.
  308. (check= (flatten-list ()) ())
  309. (check= (flatten-list '(1 2 3 4)) '(1 2 3 4))
  310. (check= (flatten-list '(1 2 (3 nil) 4)) '(1 2 3 4))
  311.  
  312.  
  313. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  314. ;; wrap-elements: List -> List
  315. ;;
  316. ;; (wrap-elements l) takes an input list l and creates a new list
  317. ;; with all elements of l put into list of size 1 (hence each element
  318. ;; of l is wrapped in a list in the returned list. See the check=
  319. ;; tests below for illustrative examples
  320. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  321. (defunc wrap-elements (l)
  322. :input-contract (listp l)
  323. :output-contract (listp (wrap-elements l))
  324. (cond ((endp l) l)
  325. (t (cons (list (first l)) (wrap-elements (rest l))))))
  326.  
  327. (check= (wrap-elements '(1 2 3)) '((1) (2) (3)))
  328. (check= (wrap-elements '(1 2 (3))) '((1) (2) ((3))))
  329.  
  330. ; Additional tests
  331. (check= (wrap-elements ()) ())
  332. (check= (wrap-elements '(1 (2 3))) '((1) ((2 3))))
  333.  
  334.  
  335. #|
  336. Part III: Discrete Math Fun
  337. Below are a set of functions that you might find useful later
  338. in the term. These help you do discrete arithmetic like you
  339. did in CS 1800.
  340. |#
  341.  
  342. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  343. ;; Define
  344. ;; rem-similar: Nat x Nat-{0} -> Nat
  345. ;;
  346. ;; (rem-similar x y) returns the remainder of the integer division of
  347. ;; x by y assuming that x and y are relatively the same size.
  348. ;; This is a helper method for (rem x y)
  349. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  350. (defunc rem-similar (x y)
  351. :input-contract (and (natp x)(posp y))
  352. :output-contract (natp (rem-similar x y))
  353. (if (< x y)
  354. x
  355. (rem-similar (- x y) y)))
  356.  
  357. (check= (rem-similar 8000000004 2000000000) 4)
  358. (check= (rem-similar 1000000356 500000000) 356)
  359. (check= (rem-similar 5000 100000) 5000)
  360. ;; The check below would be extremely slow using this method of calculating
  361. ;; the remainder
  362. ;; (check= (rem-similar 800000 2))
  363.  
  364. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  365. ; Define
  366. ; rem-smally: Nat x Nat-{0} -> Nat
  367. ;
  368. ; (rem-smally x y) returns the remainder of the integer division of
  369. ; x by y assuming that y is relatively small compared to x.
  370. ; This is a helper method for (rem x y)
  371. (defunc rem-smally (x y)
  372. :input-contract (and (natp x)(posp y))
  373. :output-contract (natp (rem-smally x y))
  374. (if (integerp (/ x y))
  375. 0
  376. (+ (rem-smally (- x 1) y) 1)))
  377.  
  378. ;; The check below would be extremely slow using this method of calculating
  379. ;; the remainder
  380. ;;(check= (rem-smally 8000000004 2000000000) 4)
  381. (check= (rem-smally 800001 2) 1)
  382. (check= (rem-smally 100000 3) 1)
  383. (check= (rem-smally 500000 2) 0)
  384. (check= (rem-smally 555555 6) 3)
  385. (check= (rem-smally 101017 10) 7)
  386.  
  387. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  388. ; GIVEN
  389. ; rem: Nat x Nat-{0} -> Nat
  390. ;
  391. ; (rem x y) returns the remainder of the integer division of x by y.
  392. ; There are two ways to calculate the remainder(both use subtraction).
  393. ; Think about what these two ways are.
  394. ; Note that for some numbers like x = 100000000 and y =11 one method is better.
  395. ; Thus we will make two definitions:
  396. ; For x and y being approximately the same size we use rem-similar
  397. ; since it is more efficient.
  398. ; For small values of y (and arbitrarily large x values), use rem-smally
  399. ; Fill in these function above. If you are curious, try calling
  400. ; (rem-smally 5000000000 4999999) and (rem-similar 5000000000 3)
  401. ; and see why we need 2 approaches.
  402. (defunc rem (x y)
  403. :input-contract (and (natp x)(posp y))
  404. :output-contract (natp (rem x y))
  405. (if (< y (/ x y))
  406. (rem-smally x y)
  407. (rem-similar x y)))
  408.  
  409. (check= (rem 2 4) 2)
  410. (check= (rem 4 2) 0)
  411. (check= (rem 16 1) 0)
  412. (check= (rem 1234567 10) 7)
  413. (check= (rem 123 48) 27)
  414.  
  415. ; Additional Checks
  416. (check= (rem 1 17) 1)
  417. (check= (rem 17 1) 0)
  418. (check= (rem 987654321 13) 4)
  419.  
  420.  
  421. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  422. ;; nat/: Nat x Nat-{0} -> Nat
  423. ;; (nat/ x y) takes a natural number x and a positive integer
  424. ;; y and returns x / y rounded down to the nearest natural number
  425. ;; HINT: If you want this function to work in logic mode later
  426. ;; in the term, how can you do division without using "/"?
  427. ;; (the function / returns a rational and ACL2s can't easily
  428. ;; prove the output is actually a natural number).
  429. ;; What is (nat/ 5 2)? What about (nat/ 7 2)? (nat/ 9 2)?
  430. ;; Do you notice a pattern.
  431. (defunc nat/ (x y)
  432. :input-contract (and (natp x)(posp y))
  433. :output-contract (natp (nat/ x y))
  434. ......
  435.  
  436. (check= (nat/ 16 3) 5)
  437. (check= (nat/ 16 2) 8)
  438.  
  439. ;; Additional tests
  440.  
  441. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  442. ;; GIVEN
  443. ;; abs: Rationalp -> Rationalp >= 0
  444. ;; (abs r) alculates the absolute value of a rational number r
  445. (defunc abs (r)
  446. :input-contract (rationalp r)
  447. :output-contract (and (rationalp (abs r))(>= (abs r) 0))
  448. (if (< r 0)
  449. (unary-- r)
  450. r))
  451.  
  452. (check= (abs -3/2) 3/2)
  453. (check= (abs 3/2) 3/2)
  454. (check= (abs -3456778/2) 3456778/2)
  455.  
  456.  
  457. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  458. ;; int/: Int x Int-{0} -> Int
  459. ;; (int/ x y) takes an integer x and a non-zero integer y
  460. ;; and returns x / y rounded down to the nearest integer value if
  461. ;; positive and rounded UP if the result is negative.
  462. ;; HINT: Do not re-invent the wheel here. Use previous functions.
  463. (defunc int/ (x y)
  464. .......
  465.  
  466.  
  467. ;; Add additional tests after these
  468. (check= (int/ -5 -4) 1)
  469. (check= (int/ 5 -4) -1)
  470. (check= (int/ 5 -4) -1)
  471. (check= (int/ 5 4) 1)
  472.  
  473. ; Additional Checks
  474.  
  475. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  476. ;; GIVEN
  477. ;; floor: Rational -> Integer
  478. ;;
  479. ;; (floor r) returns the closest integer less than rational r
  480. ;; (if r is an integer return r).
  481. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  482. (defunc floor (r)
  483. :input-contract (rationalp r)
  484. :output-contract (integerp (floor r))
  485. (let* ((absnum (abs (numerator r)))
  486. (posfloor (nat/ absnum (denominator r))))
  487. (cond ((integerp r) r)
  488. ((< (numerator r) 0) (- (unary-- posfloor) 1))
  489. (t posfloor))))
  490.  
  491.  
  492. (check= (floor 4/3) 1)
  493. (check= (floor 3/4) 0)
  494. (check= (floor 2) 2)
  495. (check= (floor -2) -2)
  496. (check= (floor -4/3) -2)
  497. (check= (floor 0) 0)
  498. (check= (floor 24/5) 4)
  499.  
  500.  
  501. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  502. ;; DEFINE
  503. ;; round: Rational -> Integer
  504. ;;
  505. ;; (round r) returns the closest integer to the rational
  506. ;; number r. For simplicity sake, any value X.Y should be rounded up
  507. ;; (the direction of positive infinity) for Y > 1/2
  508. ;; and rounded down for values of Y <= 1/2
  509. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  510. (defunc round (r)
  511. ........
  512.  
  513. (check= (round 4/3) 1)
  514. (check= (round -4/3) -1)
  515. (check= (round 5/3) 2)
  516. (check= (round -5/3) -2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement