Note that there are some explanatory texts on larger screens.

plurals
  1. POSimply Scheme. Chapter 08. Higher—Order Functions
    primarykey
    data
    text
    <p>Greets,</p> <p>Summary</p> <p>having trouble passing '(+) or '(-) as data to a cond (non evaluated). On their own, they return (+) or (-) which, as an argument returns the identity element (0).</p> <p>HELP!</p> <h2>Background.</h2> <p>For the non standard scheme in the code.</p> <p>In this book; sentences are flat lists and words are sybmols and strings. There are three higher order functions/procedures in simply.scm, part of the <a href="http://www.eecs.berkeley.edu/~bh/downloads/simply/" rel="nofollow">library</a> to illustrate the topic, every, keep and accumulate;</p> <ol> <li>(every function data) [do this function to every element of data]</li> <li>(keep predicate? data) [keep the elements of data that pass predicate? test]</li> <li>(accumulate function data) [collect all data into the form of function — combine with keep to remove invalid data] eg (accumulate + (keep number? data)) [remove non numbers then add the remaining numbers together, zero if no numbers found]</li> </ol> <h2>Data Flow.</h2> <p>Exercise <a href="http://www.eecs.berkeley.edu/~bh/ssch8/higher.html" rel="nofollow">8.11</a> is a gpa calculator procedure. By instruction, no lambda or recursion allowed (not yet taught if read sequentially).</p> <p>The first implementation I tried takes multiple grades in a single sentence and outputs individual sentences, each with a single grade. It then passes this output to a helper procedure.</p> <p>If the single grade output has a + or - it is separated, for example '(a+) into '(a) and '(+) and all output is then passed to a further helper procedure.</p> <p>then a cond allocates scores</p> <pre><code>a 4 b 3 c 2 d 1 e 0 + 0.33 - -0.33 </code></pre> <p>This, only worked in my head (why don't computers work like minds?) When a grade like '(a+) or '(a-) is seperated, the '(a) is processed properly but the '(+) or '(-) evaluate to the identity element (0) and fail to add to the gpa.</p> <p>Is there a way to make '(+) and '(-) passable as data instead of as an expression? Alternatively, can I convert them to some arbitrary data usable in the cond before they return (0)?</p> <p>The current version, a lengthy cond for each grade, works, but is hideous. Makes the implementation feel like imperative instead of functional programming.</p> <h2>Code.</h2> <p>returns the wrong gpa (doesn't add 0.33 or -0.33): also, input type check in (gpa-helper) failed spectacularly.</p> <pre><code>(define (gpa gradesset) (/ (accumulate + (every gpa-helper gradesset)) (count gradesset)) ) (define (gpa-helper gradewrd) (cond ((or (&lt; (count gradewrd) 1) (&gt; (count gradewrd) 2)) '(Please use valid grade input)) ((= (count gradewrd) 1) (gpa-allocator (keep valid-grade? gradewrd))) ((= (count gradewrd) 2) (every gpa-helper (keep valid-grade? gradewrd))) (else '(Please check that all grades entered are valid)) ) ) (define (gpa-allocator gradeletter+-) (cond ((equal? gradeletter+- 'a) 4) ((equal? gradeletter+- 'b) 3) ((equal? gradeletter+- 'c) 2) ((equal? gradeletter+- 'd) 1) ((equal? gradeletter+- 'e) 0) ((equal? gradeletter+- +) .33) ((equal? gradeletter+- -) (- .33)) (else 0) ) ) (define (valid-grade? gradein) (if (member? gradein '(+ - a+ a a- b+ b b- c+ c c- d+ d d- e)) #t #f) ) </code></pre> <p>redone version that returns a sentence of the individual scores. The 0 returned by '(+) and '(-) is visible here. Implements successful input type checking but introduces new problems. (accumulate + ing the result for one)</p> <pre><code>(define (gpa gradesset) (every gpa-cleaner gradesset) ) (define (gpa-cleaner gradewrd) (cond ((or (&lt; (count gradewrd) 1) (&gt; (count gradewrd) 2)) 0) (else (every gpa-accumulator gradewrd)) ) ) (define (gpa-accumulator gradewrd) (/ (accumulate + (every gpa-helper gradewrd)) (count gradewrd)) ) (define (gpa-helper gradewrd) (cond ((= (count gradewrd) 1) (gpa-allocator (keep valid-grade? gradewrd))) ((= (count gradewrd) 2) (every gpa-helper (keep valid-grade? gradewrd))) (else '(Please check that all grades entered are valid)) ) ) (define (gpa-allocator gradeletter+-) (cond ((equal? gradeletter+- 'a) 4) ((equal? gradeletter+- 'b) 3) ((equal? gradeletter+- 'c) 2) ((equal? gradeletter+- 'd) 1) ((equal? gradeletter+- 'e) 0) ((equal? gradeletter+- +) .33) ((equal? gradeletter+- -) (- .33)) (else 0) ) ) (define (valid-grade? gradein) (if (member? gradein '(+ - a b c d e)) #t #f) ) </code></pre> <p>Using SCM version 5e7 with Slib 3b3, the additional libraries supplied with Simply Scheme (link provided under background above — simply.scm, functions.scm, ttt.scm, match.scm, database.scm) and the library where I input my answers for every exercise loaded.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload