<div dir="ltr">I tried to implement despair 1 in Racket, and have a few questions about my solution. By the way, I am not a student trying to get my homework done by somebody else. <div><br></div><div>The restrictions for despair 1 are:<div><br></div><div>1 - The whole implementation must have less than 100 lines. Comments and tests don't count as lines</div><div><br></div><div>2- No line should be longer than 30 chars</div><div><br></div><div>3- No function should have more than 12 lines<br></div><div><br></div><div>4- A blank line after each function is mandatory</div><div><br></div><div>5- The implementation can be done in any language and must pass despair stOne.dsp</div><div><br></div><div><br></div><div>Here are two functions from stOne.dsp:</div><div><br></div><div><div>def (fib n (f-1 1) (f-2 1))</div><div>  if [n<2] then f-1</div><div>  else (fib [n - 1] </div><div>        [f-1+f-2] f-1)</div><div>  end</div><div>end</div><div><br></div><div>def (fibo n)</div><div> let loop </div><div>   local (i n) (a 1) (b 1)//</div><div>   cond</div><div>    ift [i<2] then a//</div><div>    otherwise </div><div>      (loop [i - 1] [a+b] a)//</div><div>   end</div><div> end</div><div>end</div></div><div><br></div><div>My question is about the Implementation of the infix to prefix function. My  pref macro consumed almost half of the allowed number of lines. The algorithm used is described in Lisp by Winston and Horn, third edition, chapter 32. Is there a way to reduce the size of the pref macro (perhaps changing the algorithm)?</div><div><br></div><div>In order to keep the (p e o a) function in the 12 lines limit, I was forced to move the w? predicate out of (p e o a). Since Racket match requires one parameter predicates, (w? e) returns a closure. My question is: Does Racket rebuilds the closure for each iteration of (p e o a)? If this is the case, the algorithm is very inefficient. I hope that Racket somehow builds the closure just once. Thanks in advance for any piece of information or suggestion on how to improve the program. </div><div><br></div><div>Special thanks to any member of this group who could tell me how to transform my implementation into  #lang despair (no need to maintain the restriction of 100 lines). By the way, I tried to understand the chapter about building new language in the Racket documentation, but it still puzzles me.</div><div><br></div><div><div>;File: pref.rkt</div><div>#lang racket</div><div>(require (for-syntax racket))</div><div>(provide pref)</div><div><br></div><div>(define-for-syntax(w s)</div><div> (case s ;10 lines</div><div>  [(or) 1] [(and) 2]</div><div>  [(< = > >= <=) 3]</div><div>  [(+ -) 4] [(* /) 5]</div><div>  [(^) 6][(:) 7]</div><div>  [else 9]))</div><div><br></div><div>(define-for-syntax(:? x)</div><div>  (= (w x) 9))</div><div><br></div><div> (define-for-syntax(w? e)</div><div>   (lambda(x)</div><div>     (> (w (car e))(w x))))<br></div><div><br></div><div>(define-for-syntax(pre x)</div><div>  (if (pair? x)</div><div>    (p (cdr x) '()</div><div>       (list (pre (car x))) )</div><div>      x))</div><div><br></div><div>(define-for-syntax(p e o a)</div><div> (match/values (values e o a)</div><div>  [('() '() (cons x _)) x]</div><div>  [[(cons (? :?) _) _ _ ]</div><div>   (p (cons ': e) o a)]</div><div>  [[(list-rest x y es) (or</div><div>      '()  (cons (? (w? e))_))_]  </div><div>   (p es (cons x o)</div><div>      (cons (pre y) a))]</div><div>  [[_ (cons c os)</div><div>      (list-rest x y r)]</div><div>   (p e os (f c y x r) )]))</div><div><br></div><div>(define-for-syntax(f c y x r)</div><div>  (if (equal? ': c)</div><div>      (cons (list y x) r)</div><div>      (cons (list c y x) r)))</div><div><br></div><div>(define-syntax(pref s)</div><div> (datum->syntax s (pre</div><div>   (cdr (syntax->datum s)))))</div></div><div><br></div><div><br></div><div><div>;File: despair.rkt</div><div>#lang racket</div><div>(require (for-syntax racket))</div><div>(require "inf.rkt")</div><div><br></div><div>(define-for-syntax(brk x)</div><div> (regexp-match* </div><div>  (regexp (~a "\"[^\"]+\"|"</div><div>      "#t|#f|[a-z]+->[a-z]+|"</div><div>      "[a-z?A-Z~-]+[0-9-]*|"</div><div>      "-?[0-9]+\\.?[0-9]*|"</div><div>      "<=|>=|//|"</div><div>      "[][()'&;*:<>=,+/^-]")) x))</div><div><br></div><div>(define-for-syntax (fix x)</div><div>  (match x</div><div>    ["def" "(define"]</div><div>    ["if" "(if"]</div><div>    ["then" ""]</div><div>    ["else" ""]</div><div>    ["end" ")" ]</div><div>    ["//" ")"]</div><div>    ["cond" "(cond"]</div><div>    ["ift" "("]</div><div>    ["let" "(let"]</div><div>    ["local" "("]</div><div>    ["otherwise" "(else"]</div><div>    ["[" "(pref"]</div><div>    ["]" ")"]</div><div>    [head head]))</div><div><br></div><div>(define-for-syntax (prs xpr)</div><div>  (with-input-from-string</div><div>    (string-join (for/list </div><div>       [(token (brk xpr))]</div><div>       (fix token)) " ")</div><div>    (λ() (read)) ))</div><div><br></div><div>(define-for-syntax(tkn stx)</div><div>  (~a "(" (file->string (cadr</div><div>     (syntax->datum stx)))</div><div>      ")"))</div><div><br></div><div>(define-syntax(ld stx) </div><div> (datum->syntax stx </div><div>   (cons 'begin </div><div>     (prs (tkn stx)) )))</div><div><br></div></div><div><div>(ld "stOne.dsp")</div><div>(fibo 5)</div></div></div></div>