PasteRack.org
Paste # 16524
2019-04-25 14:54:37

Fork as a new paste.

Paste viewed 151 times.


Embed:

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

=>