[Cs254f11] lets that depend on other things that have been let in the same context
Lee Spector
lspector at hampshire.edu
Fri Dec 2 14:06:21 EST 2011
FYI some lisps provide ways to do sequential bindings (like Clojure's "let") and also other ways to do bindings in which the init-expressions can be evaluated in parallel (and so they can't refer to each other). Confusingly for the polylisplingual Clojure's "let" is roughly Common Lisp's "let*" while Common Lisp's "let" does the parallel thing (semantically -- not generally using actual parallel execution).
BTW if you use a binding form that doesn't allow for sequential binding then you could always fake it by nesting calls -- I think that in some lisps the sequential version is actually a macro that expands into a nested call to the non-sequential version.
-Lee
On Dec 2, 2011, at 1:40 PM, Wm. Josiah Erikson wrote:
> Oh. Right. Duh :)
>
> I actually even knew that at some point, looking back earlier in my code. Thanks. I knew that couldn't be right - you HAVE to be able to define variables that depend on other variables.
>
> -Josiah
>
>
> On 12/2/11 12:08 PM, Lee Spector wrote:
>> Josiah,
>>
>> You just have too many brackets in there. In a "let" you can define multiple bindings, each with its own symbol and init-expression, but you should just list them all in the same single set of [] brackets. So you probably want this:
>>
>> (defn prediction-test
>> [psize gens]
>> (let [best-fit (evolve_best_fit psize gens)
>> reference-error (error best-fit)
>> test-error (test_error best-fit)
>> error-difference (Math/abs (- reference-error test-error))]
>> (println "The shortest best fit individual over" gens "generations with a population size of" psize)
>> (println "was" best-fit)
>> (println "It had an error of" reference-error "against the reference songs, and an error of")
>> (println test-error "against the test songs, giving a difference of" error-difference)))
>>
>> -Lee
>>
>>
>> On Dec 2, 2011, at 11:35 AM, Wm. Josiah Erikson wrote:
>>
>>> I find it annoying, just for readability of my program and not having to do things over and over again, that I can't do this:
>>>
>>> (defn prediction-test
>>> [psize gens]
>>> (let [best-fit (evolve_best_fit psize gens)]
>>> [reference-error (error best-fit)]
>>> [test-error (test_error best-fit)]
>>> [error-difference (Math/abs (- reference-error test-error))]
>>> (println "The shortest best fit individual over" gens "generations with a population size of" psize)
>>> (println "was" best-fit)
>>> (println "It had an error of" reference-error "against the reference songs, and an error of")
>>> (println test-error "against the test songs, giving a difference of" error-difference)))
>>>
>>> It tells me that it can't resolve the symbol reference-error in this context. I assume that this is because it defines those out of order or something. But why didn't it complain about not knowing what best-fit was first?
>>>
>>> Am I doing this wrong, or can you just not do this?
>>>
>>> --
>>> Wm. Josiah Erikson
>>> Network Engineer
>>> Hampshire College
>>> Amherst, MA 01002
>>> (413) 559-6091
>>>
>>> _______________________________________________
>>> Cs254f11 mailing list
>>> Cs254f11 at lists.hampshire.edu
>>> https://lists.hampshire.edu/mailman/listinfo/cs254f11
>> --
>> Lee Spector, Professor of Computer Science
>> Cognitive Science, Hampshire College
>> 893 West Street, Amherst, MA 01002-3359
>> lspector at hampshire.edu, http://hampshire.edu/lspector/
>> Phone: 413-559-5352, Fax: 413-559-5438
>>
>
> --
> Wm. Josiah Erikson
> Network Engineer
> Hampshire College
> Amherst, MA 01002
> (413) 559-6091
>
--
Lee Spector, Professor of Computer Science
Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspector at hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438
More information about the Cs254f11
mailing list