[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: the benefits of immutability
Hmmm, a few points off:
1. When I said `the square root function and the ToString function'
I intended that those be the `standard' Math.sqrt and Object.toString
operators. Although your `toString' and `sqrt' functions do perform
the appropriate operations, they are essentially arbitrary objects
with
the appropriate name, not the functions I asked for.
2. The returned object isn't the same kind of thing as Object.ToString,
or Math.sqrt.
3. Consider these lines:
return new Double(Math.sqrt(((Double)a).doubleValue()));}},
return ((Fn)((Object[])a)[1]).ap(((Fn)((Object[])a)[1]).ap(b));
System.out.println( ((Fn)(compose.ap(new
Object[]{sqrt,toString}))).ap(new Double(10)) );
Too many parenthesis.
> Guess I didn't get the extra credit. ;)
Programming in Java is its own reward.
----- Original Message -----
From: <vkarvone@mappi.helsinki.fi>
To: "Joe Marshall" <jrm@ccs.neu.edu>
Cc: "Vadim Nasardinov" <el-vadimo@comcast.net>; "Perry E. Metzger"
<perry@piermont.com>; <ll1-discuss@ai.mit.edu>
Sent: Tuesday, August 19, 2003 12:27
Subject: Re: the benefits of immutability
> Quoting Joe Marshall <jrm@ccs.neu.edu>:
> > [...] write the following in Java:
> >
> > (defun compose (f g)
> > (lambda (x) (funcall f (funcall g x))))
> >
> > and use it to compose the square root function and the
> > ToString function to create a function that takes numbers
> > and produces the printed representation of their square root.
>
> public class FunctionalJava {
> public static class Fn {
> public Object ap(Object a) {
> return a;
> }
> }
>
> public static final Fn
> sqrt = new Fn() {
> public Object ap(Object a) {
> return new Double(Math.sqrt(((Double)a).doubleValue()));}},
> toString = new Fn() {
> public Object ap(Object a) {
> return a.toString();}},
> compose = new Fn() {
> public Object ap(final Object a) {
> return new Fn() {
> public Object ap(Object b) {
> return
((Fn)((Object[])a)[1]).ap(((Fn)((Object[])a)[1]).ap(b));
> }
> };
> }
> };
>
> public static void main(String argv[]) {
> System.out.println( ((Fn)(compose.ap(new
> Object[]{sqrt,toString}))).ap(new Double(10)) );
> }
> }
>
> > Extra credit if you can do it in 240 characters. (three 80 character
> > lines)
>
>
> Regards,
> Vesa Karvonen
>