At 03:08 PM 3/27/2007, you wrote:
Hi all,
James threw down the gauntlet yesterday with respect to doing this in
Groovy. I know it can be done, and I plan on working on this this
weekend just for fun. Here's the challenge:
http://legacyofthemob.se/istari/2006/10/08/higher-order-messaging-in- ruby/

I think that this would be a fun exercise (for those of us inclined
to do so and flex our coding muscles so to speak). Pick a language
(Scala, Haskell, Erlang, Groovy, Smalltalk, Objective-C, Lisp, anyone
up for Dylan?, etc.) and then post your findings either here, on the
wiki (if we get enough), or your blog.

What fun!

Function application and mapping are trivial in functional languages.
Here are some examples in Scheme. I've used the Kawa interpreter,
which translates to JVM code (http://www.gnu.org/software/kawa/):

#|kawa:1|#  (set! aLst '(-1 0 1 -2 4))

#|kawa:2|#  (apply + aLst)
2

#|kawa:2|#  (apply - aLst)
-4

#|kawa:4|# (map zero? aLst)
(#f #t #f #f #f)

#|kawa:4|# (map positive? aLst)
(#f #f #t #f #t)


(define collect (lambda (fn args)
  (letrec ( (res ())
            (do1 (lambda (res fn args)
              (cond ((null? args) res)
                    ((fn (car args))
                     (do1 (append res (list (car args))) fn (cdr args)))
                    (else (do1 res fn (cdr args))))))
          )
    (do1 res fn args)
  )
))

#|kawa:6|#  (collect zero? '(1 2 3 4))
()

#|kawa:67#  (collect zero? aLst)
(0)

#|kawa:67#  (collect positive? aLst)
(1 4)



 And I'm sure that Bill will
figure out a way to do this in Icon ;-).

Well, Icon is not an OO language so the problem is somewhat
different. It does, however, have procedures as first-class objects,
which allows us to achieve interesting effects in a manner similar to Lisp:

procedure main ()
  aLst := [-1, 0, 1, -2, 4]
  write(apply(plus, aLst))
  write(apply(minus, aLst))
  write(showList(map(zeroP, aLst)))
  write(showList(map(positiveP, aLst)))
  write(showList(map(showList, aLst)))
end

procedure plus (x, y)
  return x + y
end

procedure minus (x, y)
  return x - y
end

procedure zeroP (x)
  return 0 == x
end

procedure positiveP (x)
  return 0 < x
end

procedure apply (fn, args)
   res := !args
   every res := fn(res, args[2 to *args])
   return res
end

procedure map (fn, args)
   res := []
   every put(res, \fn(!args))
   return res
end

procedure showList (lst)
  out := "["
  every out ||:= !lst do
    out ||:= ","
  if (out[-1] == ",") then
    out[-1] := "]"
  else
    out ||:= "]"
  return out
end

The output of this program is:

2
-4
[0]
[1,4]
[[-,1],[0],[1],[-,2],[4]]

Note the last result, which comes from (frivolously) mapping the helper procedure
'showList' to the data list (just to show it can be done).



BTW - I feel that it is this kind of language feature that makes it
easier to write an Internal DSL in a given language than on top of
something like Java.

Yea...definitely.
        regards,
        -tom




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to