Difference between revisions of "Dynamic Spreadsheet Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
(Created page with "=Motivation= =Code To Use= * [https://docs.racket-lang.org/reference/booleans.html #t] true * [https://docs.racket-lang.org/reference/booleans.html #f] false * [https://docs....")
 
 
(22 intermediate revisions by the same user not shown)
Line 2: Line 2:
  
 
=Code To Use=
 
=Code To Use=
* [https://docs.racket-lang.org/reference/booleans.html #t] true
+
==general==
* [https://docs.racket-lang.org/reference/booleans.html #f] false
+
* [https://docs.racket-lang.org/reference/eval.html#%28def._%28%28quote._~23~25kernel%29._eval%29%29 eval]
* [https://docs.racket-lang.org/reference/define.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._define-syntax%29%29 define-syntax]
+
* [https://docs.racket-lang.org/reference/lambda.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._lambda%29%29 lambda]
* [https://docs.racket-lang.org/reference/lambda.html?q=lambda#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._lambda%29%29 lambda]
+
* [https://docs.racket-lang.org/reference/if.html#%28form._%28%28quote._~23~25kernel%29._if%29%29 if]
* [https://docs.racket-lang.org/reference/procedures.html#%28def._%28%28quote._~23~25kernel%29._procedure~3f%29%29 procedure?]
+
* [https://docs.racket-lang.org/reference/if.html#%28form._%28%28lib._racket%2Fprivate%2Fletstx-scheme..rkt%29._and%29%29 and]
* [https://docs.racket-lang.org/reference/procedures.html?q=procedure-arity#%28def._%28%28quote._~23~25kernel%29._procedure-arity%29%29 procedure-arity] number of arguments a function takes [https://en.wikipedia.org/wiki/Arity Arity on Wikipedia]
+
==void==
* [https://docs.racket-lang.org/reference/pairs.html?q=cons#%28def._%28%28quote._~23~25kernel%29._pair~3f%29%29 pair?]
+
* [https://docs.racket-lang.org/reference/void.html#%28def._%28%28quote._~23~25kernel%29._void%29%29 void]
 +
==number==
 +
* [https://docs.racket-lang.org/reference/number-types.html#%28def._%28%28quote._~23~25kernel%29._zero~3f%29%29 zero?]
 +
* [https://docs.racket-lang.org/reference/generic-numbers.html#%28def._%28%28quote._~23~25kernel%29._string-~3enumber%29%29 string->number]
 +
* [https://docs.racket-lang.org/reference/number-types.html#%28def._%28%28quote._~23~25kernel%29._number~3f%29%29 number?]
 +
==string==
 +
* [https://docs.racket-lang.org/reference/strings.html#%28def._%28%28quote._~23~25kernel%29._string-length%29%29 string-length]
 +
* [https://docs.racket-lang.org/reference/strings.html#%28def._%28%28lib._racket%2Fstring..rkt%29._string-prefix~3f%29%29 string-prefix?]
 +
* [https://docs.racket-lang.org/reference/strings.html#%28def._%28%28lib._racket%2Fstring..rkt%29._string-suffix~3f%29%29 string-suffix?]
 +
==list==
 +
* [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28quote._~23~25kernel%29._list-ref%29%29 list-ref] returns element of list at position
 
* [https://docs.racket-lang.org/reference/pairs.html?q=cons#%28def._%28%28quote._~23~25kernel%29._cons%29%29 cons]
 
* [https://docs.racket-lang.org/reference/pairs.html?q=cons#%28def._%28%28quote._~23~25kernel%29._cons%29%29 cons]
* [https://docs.racket-lang.org/reference/pairs.html?q=cons#%28def._%28%28quote._~23~25kernel%29._car%29%29 car]
+
* [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Fmap..rkt%29._map%29%29 map]
* [https://docs.racket-lang.org/reference/pairs.html?q=cons#%28def._%28%28quote._~23~25kernel%29._cdr%29%29 cdr]
+
* [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Flist..rkt%29._foldl%29%29 foldl]
* [https://docs.racket-lang.org/reference/values.html#%28def._%28%28quote._~23~25kernel%29._values%29%29 values]
+
* [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Flist..rkt%29._foldr%29%29 foldr]
* [https://docs.racket-lang.org/reference/exns.html?q=raise#%28def._%28%28quote._~23~25kernel%29._raise%29%29 raise]
+
* [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Flist..rkt%29._reverse%29%29 reverse]
 +
* [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28quote._~23~25kernel%29._length%29%29 length]
  
 
=Code To Implement=
 
=Code To Implement=
{{RacketToImplement|spreadsheet|to-cell<br>csv->spreadsheet<br>row-count<br>row-at sheet<br>sum-row|spreadsheet}}
+
{{RacketToImplement|spreadsheet|string->cell<br>csv->spreadsheet<br>row-count<br>row-at sheet<br>sum-row|spreadsheet}}
==to-cell==
+
==string->cell==
define a function <code>thunk?</code> which returns whether the specified parameter is a thunk or not.
+
(define (string->cell s)
 +
        (error 'not-yet-implemented))
  
<nowiki>(define (thunk? th)
+
* if s is the empty string, evaluate to void
    (error 'not-yet-implemented))</nowiki>
+
* if s represents a number, evaluate to that number
 +
* if s starts with "(" and ends with ")", evaluate to a form suited for eval
 +
* otherwise, evaluate to s
  
===csv->spreadsheet===
+
==csv->spreadsheet==
define a '''MACRO''' <code>thunk-that</code> which takes a parameter <code>e</code> creates thunk.  that is: wraps <code>e</code> in a zero argument function.
+
(define (csv->spreadsheet matrix)
 +
        (error 'not-yet-implemented))
  
(define-syntax-rule (thunk-that e)
+
given a list of lists of strings, evaluate to a list of list of cells using [[#string->cell]] and a couple of list higher order functions.
    (error 'not-yet-implemented))
 
  
Thunks are useful for delaying the evaluation of expressions. As such <code>thunk-that</code> must be declared as a macro and not a function.  Unlike [https://www.haskell.org/ Haskell] which has lazy evaluation, Racket (and most other languages) [https://en.wikipedia.org/wiki/Eager_evaluation eagerly evaluates] function arguments.
+
==row-count==
 +
  (define (row-count sheet)
 +
        (error 'not-yet-implemented))
  
===row-count===
+
<code>hockey-spreadsheet</code>:
define a function <code>dethunk</code> which takes a thunk parameter <code>e</code> and returns the result of invoking <code>e</code>.
+
{| class="wikitable"
 +
|Name
 +
|Uniform Number
 +
|Birth Year
 +
|Games Played
 +
|Goals
 +
|Assists
 +
|-
 +
|Bobby Orr
 +
|4
 +
|1948
 +
|657
 +
|270
 +
|645
 +
|-
 +
|Wayne Gretzky
 +
|99
 +
|1961
 +
|1487
 +
|894
 +
|1963
 +
|-
 +
|Mario Lemieux
 +
|66
 +
|1965
 +
|915
 +
|690
 +
|1033
 +
|}
  
(define (dethunk-that thunk)
+
<code>('''row-count''' hockey-spreadsheet)</code> evaluates to <code>'''4'''</code>
    (error 'not-yet-implemented))
 
  
If thunking and expression wraps an expression in a single argument function, then de-thunking is simply calling that function.
+
==row-at==
 +
(define (row-at sheet row-index)
 +
        (error 'not-yet-implemented))
  
If the parameter <code>thunk</code> is not a <code>thunk?</code> then <code>dethunk-that</code> should [https://docs.racket-lang.org/reference/exns.html#%28def._%28%28quote._~23~25kernel%29._raise-argument-error%29%29 raise an argument error].
+
<code>('''row-at''' hockey-spreadsheet 1)</code> evaluates to <code>'''(list "Bobby Orr" 4 1948 657 270 645)'''</code>
  
NOTE: It may seem unnecessary to use <code>dethunk-that</code> when implementing Lab4, when you could simply (thunk)... that is "call the thunk".  Still, you are encouraged to use <code>dethunk-that</code> as a bit of verbosity can sometimes help in debugging a sea already full of parentheses.
+
==sum-row==
 +
(define (sum-row sheet row-index)
 +
        (error 'not-yet-implemented))
  
===row-at===
+
<code>('''sum-row''' hockey-spreadsheet 1)</code> evaluates to <code>'''3524'''</code>
  
===sum-row===
+
<code>function-spreadsheet</code>:
define a function <code>destream</code> which takes a <code>stream</code> parameter and a pair of the next answer and the next stream.
+
{| class="wikitable"
 +
|<code>(+ 1 2)</code>
 +
|<code>(+ 3 4)</code>
 +
|-
 +
|<code>(/ 10 2)</code>
 +
|<code>(sqrt (/ 1 4))</code>
 +
|-
 +
|<code>(sin (/ 3.14159 4))</code>
 +
|<code>10</code>
 +
|}
  
(define (destream stream)
+
<code>('''sum-row''' function-spreadsheet 0)</code> evaluates to <code>'''10'''</code>
    (error 'not-yet-implemented))
 
  
A critical value of this utility function is to provide early error detection.  As such, exceptions should be raised if either
+
<code>('''sum-row''' function-spreadsheet 1)</code> evaluates to <code>'''5 1/2'''</code>
  
* the parameter <code>s</code> is not a thunk (this can be handled by <code>dethunk-that</code>)
+
<code>('''sum-row''' function-spreadsheet 2)</code> evaluates to <code>'''10.707106312093558'''</code>
* the result of de-thunking the stream is not a pair
 
* next-stream of the pair is not thunk
 
<!--
 
Example use of  [https://docs.racket-lang.org/reference/values.html#%28def._%28%28quote._~23~25kernel%29._values%29%29 values] and [https://docs.racket-lang.org/reference/define.html?q=define-values#%28form._%28%28quote._~23~25kernel%29._define-values%29%29 define-values]:
 
 
 
<youtube>iWCcL049dOg</youtube>
 
 
 
<nowiki>(define (div-mod-values n d)
 
  (values (quotient n d) (modulo n d)))
 
 
 
(define (printf-div-mod-values n d)
 
  (local [(define-values (q r) (div-mod-values n d))]
 
    (printf "~a/~a => ~a w/ remainder ~a\n" n d q r)))
 
 
 
(printf-div-mod-values 425 231)</nowiki>
 
 
 
produces the output:
 
 
 
<nowiki>425/231 => 1 w/ remainder 194</nowiki>
 
-->
 
===stream-cons-ensuring-stream-prime-is-thunk===
 
Define a function <code>cons-with-thunk-check-on-next-stream</code> which takes two parameters <code>value</code> and <code>next-stream</code>. 
 
 
 
((define (cons-with-thunk-check-on-next-stream element next-stream)
 
  (error 'not-yet-implemented))
 
 
 
If the specified <code>next-stream</code> parameter is not a thunk then an error should be raised:
 
 
 
<code>(raise-argument-error "next-stream" "thunk?" next-stream)</code>
 
 
 
Do '''NOT''' create a thunk.  If <code>next-stream</code> is a thunk it should simply [https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28quote._~23~25kernel%29._cons%29%29 cons] <code>value</code> and <code>next-stream</code>.
 
  
 
=Test=
 
=Test=
 
{{RacketUnitTest|spreadsheet_test|spreadsheet}}
 
{{RacketUnitTest|spreadsheet_test|spreadsheet}}

Latest revision as of 14:16, 30 March 2022

Motivation

Code To Use

general

void

number

string

list

Code To Implement

file: src/main/racket/spreadsheet/spreadsheet.rkt Racket-logo.svg
functions: string->cell
csv->spreadsheet
row-count
row-at sheet
sum-row

string->cell

(define (string->cell s)
       (error 'not-yet-implemented))
  • if s is the empty string, evaluate to void
  • if s represents a number, evaluate to that number
  • if s starts with "(" and ends with ")", evaluate to a form suited for eval
  • otherwise, evaluate to s

csv->spreadsheet

(define (csv->spreadsheet matrix)
       (error 'not-yet-implemented))

given a list of lists of strings, evaluate to a list of list of cells using #string->cell and a couple of list higher order functions.

row-count

(define (row-count sheet)
       (error 'not-yet-implemented))

hockey-spreadsheet:

Name Uniform Number Birth Year Games Played Goals Assists
Bobby Orr 4 1948 657 270 645
Wayne Gretzky 99 1961 1487 894 1963
Mario Lemieux 66 1965 915 690 1033

(row-count hockey-spreadsheet) evaluates to 4

row-at

(define (row-at sheet row-index)
       (error 'not-yet-implemented)) 

(row-at hockey-spreadsheet 1) evaluates to (list "Bobby Orr" 4 1948 657 270 645)

sum-row

(define (sum-row sheet row-index)
       (error 'not-yet-implemented))

(sum-row hockey-spreadsheet 1) evaluates to 3524

function-spreadsheet:

(+ 1 2) (+ 3 4)
(/ 10 2) (sqrt (/ 1 4))
(sin (/ 3.14159 4)) 10

(sum-row function-spreadsheet 0) evaluates to 10

(sum-row function-spreadsheet 1) evaluates to 5 1/2

(sum-row function-spreadsheet 2) evaluates to 10.707106312093558

Test

file: spreadsheet_test.rkt Racket-logo.svg Test
source folder: src/test/racket/spreadsheet

note: ensure that you have removed all printing to receive credit for any assignment.