PasteRack.org
Paste # 60554
2019-04-25 14:59:15

Fork as a new paste.

Paste viewed 155 times.


Embed:

  1. #lang racket
  2.  
  3.  
  4. ; a card has a suit and a rank
  5. ; suit = 'hearts | 'diamonds | 'spades | 'clubs
  6. ; rank = 2-10 | 'jack | 'queen | 'king | 'ace
  7. (struct card (suit rank) #:transparent)
  8.  
  9. (define aceOfSpades (card 'spades 'ace))
  10. (define threeOfHearts (card 'hearts 3))
  11. (define alsoThreeOfHearts (card 'hearts 3))
  12. (define jackOfDiamonds (card 'diamonds 'jack))
  13. (define sevenOfClubs (card 'clubs 7))
  14.  
  15. ;
  16. ; 1. Rearrange the lines of the cardColor function to work correctly
  17. ; the function cardColor takes a card as an argument and returns its color (spades and clubs are black; diamonds and hearts are red)
  18. ;
  19.  
  20. ;(define (cardColor c)
  21. ;          (eq? (card-suit c) 'diamonds))
  22. ;  (if (or (eq? (card-suit c) 'hearts)
  23. ;      'black))
  24. ;      'red
  25.  
  26. (define
  27.     (if (or (eq? (card-suit c) 'hearts)
  28.         (eq? (card-suit c) 'diamonds))
  29.     'red
  30.     'black))
  31.  
  32.  
  33.  
  34. (cardColor aceOfSpades)
  35. (cardColor threeOfHearts)
  36. (cardColor jackOfDiamonds)
  37. (cardColor sevenOfClubs)
  38.  
  39. ;
  40. ; 2. Rearrange the lines of the cardValue function to work correctly
  41. ; the function cardValue takes a card and returns its value (numbered cards have their number as the value, aces are 11, everything else is 10
  42. ;
  43.  
  44. ;        [(eq? (card-rank c) 'queen) 10]
  45. ;        [#t (card-rank c)]))
  46. ;        [(eq? (card-rank c) 'king) 10]
  47. ;(define (cardValue c)
  48. ;        [(eq? (card-rank c) 'ace) 11]
  49. ;  (cond [(eq? (card-rank c) 'jack) 10]
  50.  
  51. (define (cardValue c)
  52.     (cond [(eq? (card-rank c) 'jack) 10]
  53.           [(eq? (card-rank c) 'queen) 10]
  54.           [(eq? (card-rank c) 'king) 10]
  55.           [(eq? (card-rank c) 'ace) 11]
  56.           [#t (card-rank c)]))
  57.  
  58.  
  59.  
  60.  
  61.  
  62. (cardValue aceOfSpades)
  63. (cardValue threeOfHearts)
  64. (cardValue jackOfDiamonds)
  65. (cardValue sevenOfClubs)
  66.  
  67. ;
  68. ; 3. Rearrange the lines of the removeCard fuction to work correctly
  69. ; the function removeCard takes a list of cards cs, and a card c. It returns a list that has all the elements of cs except c.
  70. ; Compare cards with equal? instead of eq?
  71. ;
  72.  
  73. ;      null
  74. ;  (if (null? cs)
  75. ;          (cons (car cs) (removeCard (cdr cs) c)))))
  76. ;      (if (equal? (car cs) c)
  77. ;(define (removeCard cs c)
  78. ;          (removeCard (cdr cs) c)
  79.  
  80. (define (removeCard cs c)
  81.     (if (null? cs)
  82.         null
  83.         (if (equal? (car cs) c)
  84.             (removeCard (cdr cs) c)
  85.             (cons (car cs) (removeCard (cdr cs) c)))))
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. (define listOfCards (list aceOfSpades threeOfHearts alsoThreeOfHearts jackOfDiamonds threeOfHearts sevenOfClubs aceOfSpades))
  95. (removeCard listOfCards threeOfHearts)
  96.  
  97. ;
  98. ; 4. Rearrange the lines of the allSameColor function
  99. ; the function allSameColor takes a list of cards and returns true if all the cards in the list are the same color
  100. ;
  101.  
  102. ;           (allSameColor (cdr cs)))))
  103. ;(define (allSameColor cs)
  104. ;      (and (eq? (cardColor (car cs)) (cardColor (car (cdr cs))))
  105. ;  (if (null? (cdr cs))
  106. ;      #t
  107.  
  108. (define (allSameColor cs)
  109.     (if (null? (cdr cs))
  110.         #t
  111.          (and (eq? (cardColor (car cs)) (cardColor (car (cdr cs))))
  112.             (allSameColor (cdr cs)))))
  113.  
  114.  
  115.  
  116.  
  117. (allSameColor listOfCards)
  118. (define anotherListOfCards (list threeOfHearts alsoThreeOfHearts jackOfDiamonds))
  119. (allSameColor anotherListOfCards)
  120.  
  121. ;
  122. ; 5. Rearrange the lines of the sumCards function
  123. ; function sumCards takes a list of cards and returns the sum of their values.
  124. ;
  125.  
  126. ;(define (sumCards cs)
  127. ;      (+ (cardValue (car cs)) (sumCards (cdr cs)))))
  128. ;  (if (null? cs)
  129. ;      0
  130.  
  131. (define (sumCards cs)
  132.     (if (null? cs)
  133.         0
  134.         (+ (cardValue (car cs)) (sumCards (cdr cs)))))
  135.  
  136.  
  137.  
  138.  
  139. (sumCards listOfCards)
  140.  
  141. ;
  142. ; Fold reduces the items in a list to a single value
  143. ;
  144.  
  145. (define (fold f acc xs)
  146.   (if (null? xs)
  147.       acc
  148.       (fold f (f acc (car xs)) (cdr xs))))
  149.  
  150. ;
  151. ; An example of using fold to implement sameColor
  152. ;
  153.  
  154. (define (sameColor? c)
  155.   (lambda (acc c2) (and
  156.                     acc
  157.                     (eq? (cardColor c) (cardColor c2)))))
  158.  
  159. (define (allSameColor2 cs)
  160.   (fold (sameColor? (car cs)) #t cs))
  161.  
  162. (allSameColor2 listOfCards)
  163. (allSameColor2 anotherListOfCards)
  164.  
  165. ;
  166. ; Challenge problem: write sumCards2 to use fold
  167.  
  168. ;(define (sumCards2 cs)
  169. ;  (fold...))
  170.  
  171. (define (sumCards2)
  172.     (fold (lambda (acc c) (+ acc (cardValue c))) 0 cs))
  173. (sumCards2 listOfCards)

=>