PasteRack.org
Paste # 24600
2019-04-23 14:45:54

Fork as a new paste.

Paste viewed 63 times.


Embed:

  1. #lang racket
  2. (define(symbol stock)
  3.   (car stock))
  4. (define(price stock)
  5.   (car(cdr stock)))
  6. (define(shares stock)
  7.   (car(cdr(cdr stock))))
  8.  
  9. (define (printValue stock)
  10.   (printf "~a : ~a~%" (symbol stock) (* (price stock) (shares stock))))
  11.  
  12. (define(value stock)
  13.   (*(price stock) (shares stock)))
  14.  
  15. (define appleStock(list "AAPL" 100 5))
  16.  
  17.  
  18. (printValue appleStock)
  19.  
  20. (define (compareValue stock1 stock2)
  21.   (if(> (value stock1) (value stock2))
  22.         (symbol stock1)
  23.         (symbol stock2)))
  24. (define ibmStock(list "IBM" 30 10))
  25. (compareValue appleStock ibmStock)
  26.  
  27. (define(findMaxValue stocks)
  28.   (if(null?(cdr stocks))
  29.      (car stocks)
  30.      (let([s1(car stocks)]
  31.           [maxTail(findMaxValue(cdr stocks))])
  32.        (if(compareValue s1 maxTail)
  33.           s1
  34.           maxTail))))
  35. (define googleStock(list "GOOG" 70 15))
  36. (define facebookStock(list "FB" 20 8))
  37. (define stockList(list appleStock ibmStock googleStock facebookStock))
  38. (findMaxValue stockList)
  39.  
  40. (define (my-delay f)
  41.   (mcons #f f))
  42. (define (my-force th)
  43.   (if(mcar th)
  44.      (mcdr th)
  45.      (begin (set-mcar! th #t)
  46.             (set-mcdr! th ((mcdr th)))
  47.             (mcdr th))))
  48.  
  49. (define appleValue(my-delay(lambda() (value appleStock))))
  50.   (my-force appleValue)
  51.   (my-force appleValue)
  52.  
  53. (define (compareValue2 s1Value s2Value)
  54.   (>(my-force s1Value) (my-force s2Value)))
  55.  
  56. (define (findMaxValue2 stocks)
  57.   (if(null? (cdr stocks))
  58.             (car stocks)
  59.             (let* ([s1 (car stocks)]
  60.                    [s1Value (my-delay (lambda() (value s1)))]
  61.                    [maxTail (findMaxValue2 (cdr stocks))]
  62.                    [maxTailValue(my-delay (lambda()(value maxTail)))])
  63.               (if(compareValue2 s1Value maxTailValue)
  64.                  s1
  65.                  maxTail))))
  66. (findMaxValue2 stockList)
  67.  
  68. (define (map f xs)
  69.   (if (null? xs)
  70.       null
  71.       (cons(f(car xs)) (map f (cdr xs)))))
  72. (define xs '(1 2 3 4))
  73. (define f(lambda (x) (+ x 1)))
  74. (map(lambda(x) (+ x 1)) xs)
  75.  
  76. (define(filter f xs)
  77.   (if(null? xs)
  78.      null
  79.      (if(f(car xs))
  80.         (cons (car xs) (filter f (cdr xs)))
  81.         (filter f (cdr xs)))))

=>

AAPL : 500

"AAPL"

'("AAPL" 100 5)

500

500

'("GOOG" 70 15)

'(2 3 4 5)