Hello again- <div><br></div><div>I suppose this doesn't look like a case for eval when you first look into it, but since I would like to export the shorthand symbols to a file, I just might need eval. </div><div><br></div>
<div>- Henry. <span></span><br><br>Em domingo, 27 de julho de 2014, Pierpaolo Bernardi <<a href="mailto:olopierpa@gmail.com">olopierpa@gmail.com</a>> escreveu:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Sun, Jul 27, 2014 at 5:10 AM, Henry Lenzi <<a href="javascript:;" onclick="_e(event, 'cvml', 'henry.lenzi@gmail.com')">henry.lenzi@gmail.com</a>> wrote:<br>
> #lang racket<br>
><br>
> #|<br>
><br>
> Dear Racketeers -<br>
><br>
> I would like to create a little function<br>
> that would write a line of a medical recipe.<br>
><br>
> I would pass to "read" a shorthand form,<br>
> and the program would expand the text. Like so:<br>
><br>
> When I answer "hctz25" and "30", it would print:<br>
> "Hydrochlorothiazide 25mg ----------- 30 pills".<br>
><br>
> But I'm facing problems related to:<br>
> - use of "map" with "eval" in the definition area (namespace issues...)<br>
> See below.<br>
> |#<br>
><br>
><br>
> (require scheme/base) ; Is this necessary?<br>
> (define this-namespace (make-base-namespace))<br>
><br>
> (define hctz25 "Hydroclorothiazide 25mg")<br>
> (define line "-----------")<br>
> (define pl "pills")<br>
><br>
> (define *med-name* 'NIL) ; *med-name* has global scope - this is on purpose<br>
> (define *med-name-str* "") ; *med-name-str* has global scope<br>
><br>
> (define (ask-med-name)<br>
> (print "Medication") (newline)<br>
> (set! *med-name* (read))<br>
> (set! *med-name-str* (symbol->string *med-name*)))<br>
><br>
> (define *quant* 0) (define *quant-str* " ")<br>
><br>
> (define (ask-quant)<br>
> (print "Quantity") (newline)<br>
> (and<br>
> (set! *quant* (read)) (unless (number? *quant*) (error "It's not a<br>
> number!")))<br>
> (set! *quant-str* (number->string *quant*)))<br>
><br>
> (define (ask)<br>
> (ask-med-name)<br>
> (ask-quant))<br>
> (ask)<br>
><br>
> #|<br>
> Where's the problem?<br>
> In the REPL, I can do this:<br>
><br>
>> (map eval '((eval *med-name*) line *quant-str* pl))<br>
> '("Hydrochlorothiazide 25mg" "-----------" "30" "pills")<br>
><br>
> or this:<br>
><br>
>> (string-join (map eval '((eval *med-name*) line *quant-str* pl)))<br>
><br>
> "Hydrochlorothiazide 25mg ----------- 30 pills"<br>
><br>
><br>
> But, because of the namespace issues associated with "eval", I'm not<br>
> able to do it in the definition area.<br>
><br>
> I have no clue as to what the syntax should be.<br>
<br>
This is not a case where eval is useful (very few of such cases exist).<br>
<br>
Here's a hint:<br>
<br>
#lang racket<br>
<br>
(define abbreviations<br>
(hash 'hctz25 "Hydroclorothiazide 25mg"<br>
;; more abbreviations here<br>
))<br>
<br>
(define (print-a-line med quant)<br>
(printf "~a ---- ~a pills~n"<br>
(hash-ref abbreviations med "uh?")<br>
quant))<br>
<br>
then:<br>
<br>
> (print-a-line 'hctz25 30)<br>
Hydroclorothiazide 25mg ---- 30 pills<br>
<br>
<br>
I hope this will get you started<br>
<br>
P.<br>
</blockquote></div>