PasteRack.org
Paste # 4356
2019-04-25 14:22:51

Fork as a new paste.

Paste viewed 50 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. (cardColor aceOfSpades)
  26. (cardColor threeOfHearts)
  27. (cardColor jackOfDiamonds)
  28. (cardColor sevenOfClubs)
  29.  
  30. ;
  31. ; 2. Rearrange the lines of the cardValue function to work correctly
  32. ; 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
  33. ;
  34.  
  35. ;        [(eq? (card-rank c) 'queen) 10]
  36. ;        [#t (card-rank c)]))
  37. ;        [(eq? (card-rank c) 'king) 10]
  38. ;(define (cardValue c)
  39. ;        [(eq? (card-rank c) 'ace) 11]
  40. ;  (cond [(eq? (card-rank c) 'jack) 10]
  41.  
  42. (cardValue aceOfSpades)
  43. (cardValue threeOfHearts)
  44. (cardValue jackOfDiamonds)
  45. (cardValue sevenOfClubs)
  46.  
  47. ;
  48. ; 3. Rearrange the lines of the removeCard fuction to work correctly
  49. ; 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.
  50. ; Compare cards with equal? instead of eq?
  51. ;
  52.  
  53. ;      null
  54. ;  (if (null? cs)
  55. ;          (cons (car cs) (removeCard (cdr cs) c)))))
  56. ;      (if (equal? (car cs) c)
  57. ;(define (removeCard cs c)
  58. ;          (removeCard (cdr cs) c)
  59.  
  60. (define listOfCards (list aceOfSpades threeOfHearts alsoThreeOfHearts jackOfDiamonds threeOfHearts sevenOfClubs aceOfSpades))
  61. (removeCard listOfCards threeOfHearts)
  62.  
  63. ;
  64. ; 4. Rearrange the lines of the allSameColor function
  65. ; the function allSameColor takes a list of cards and returns true if all the cards in the list are the same color
  66. ;
  67.  
  68. ;           (allSameColor (cdr cs)))))
  69. ;(define (allSameColor cs)
  70. ;      (and (eq? (cardColor (car cs)) (cardColor (car (cdr cs))))
  71. ;  (if (null? (cdr cs))
  72. ;      #t
  73.  
  74. (allSameColor listOfCards)
  75. (define anotherListOfCards (list threeOfHearts alsoThreeOfHearts jackOfDiamonds))
  76. (allSameColor anotherListOfCards)
  77.  
  78. ;
  79. ; 5. Rearrange the lines of the sumCards function
  80. ; function sumCards takes a list of cards and returns the sum of their values.
  81. ;
  82.  
  83. ;(define (sumCards cs)
  84. ;      (+ (cardValue (car cs)) (sumCards (cdr cs)))))
  85. ;  (if (null? cs)
  86. ;      0
  87.  
  88. (sumCards listOfCards)
  89.  
  90. ;
  91. ; Fold reduces the items in a list to a single value
  92. ;
  93.  
  94. (define (fold f acc xs)
  95.   (if (null? xs)
  96.       acc
  97.       (fold f (f acc (car xs)) (cdr xs))))
  98.  
  99. ;
  100. ; An example of using fold to implement sameColor
  101. ;
  102.  
  103. (define (sameColor? c)
  104.   (lambda (acc c2) (and
  105.                     acc
  106.                     (eq? (cardColor c) (cardColor c2)))))
  107.  
  108. (define (allSameColor2 cs)
  109.   (fold (sameColor? (car cs)) #t cs))
  110.  
  111. (allSameColor2 listOfCards)
  112. (allSameColor2 anotherListOfCards)
  113.  
  114. ;
  115. ; Challenge problem: write sumCards2 to use fold
  116.  
  117. ;(define (sumCards2 cs)
  118. ;  (fold...))
  119.  
  120. (sumCards2 listOfCards)

=>

cardColor: undefined;

 cannot reference an identifier before its definition

  in module: 'm

cardColor: undefined;

 cannot reference an identifier before its definition

  in module: 'm

cardColor: undefined;

 cannot reference an identifier before its definition

  in module: 'm

cardColor: undefined;

 cannot reference an identifier before its definition

  in module: 'm

cardValue: undefined;

 cannot reference an identifier before its definition

  in module: 'm

cardValue: undefined;

 cannot reference an identifier before its definition

  in module: 'm

cardValue: undefined;

 cannot reference an identifier before its definition

  in module: 'm

cardValue: undefined;

 cannot reference an identifier before its definition

  in module: 'm

removeCard: undefined;

 cannot reference an identifier before its definition

  in module: 'm

allSameColor: undefined;

 cannot reference an identifier before its definition

  in module: 'm

allSameColor: undefined;

 cannot reference an identifier before its definition

  in module: 'm

sumCards: undefined;

 cannot reference an identifier before its definition

  in module: 'm

cardColor: undefined;

 cannot reference an identifier before its definition

  in module: 'm

cardColor: undefined;

 cannot reference an identifier before its definition

  in module: 'm

sumCards2: undefined;

 cannot reference an identifier before its definition

  in module: 'm