What is Expressiveness in a Computer Language
Greg Buchholz
sleepingsquirrel at yahoo.com
Tue Jun 27 19:12:06 EDT 2006
Joe Marshall wrote:
> It isn't clear to me which programs we would have to give up, either.
> I don't have much experience in sophisticated typed languages. It is
> rather easy to find programs that baffle an unsophisticated typed
> language (C, C++, Java, etc.).
>
> Looking back in comp.lang.lisp, I see these examples:
>
> (defun noisy-apply (f arglist)
> (format t "I am now about to apply ~s to ~s" f arglist)
> (apply f arglist))
Just for fun, here is how you might do "apply" in Haskell...
{-# OPTIONS -fglasgow-exts #-}
class Apply x y z | x y -> z where
apply :: x -> y -> z
instance Apply (a->b) a b where
apply f x = f x
instance Apply b as c => Apply (a->b) (a,as) c where
apply f (x,xs) = apply (f x) xs
f :: Int -> Double -> String -> Bool -> Int
f x y z True = x + floor y * length z
f x y z False= x * floor y + length z
args = (1::Int,(3.1415::Double,("flub",True)))
main = print $ apply f args
...which depends on the fact that functions are automatically curried
in Haskell. You could do something similar for fully curried function
objects in C++. Also of possible interest...
Functions with the variable number of (variously typed) arguments
http://okmij.org/ftp/Haskell/types.html#polyvar-fn
...But now I'm curious about how to create the "apply" function in a
language like Scheme. I suppose you could do something like...
(define (apply fun args)
(eval (cons fun args)))
...but "eval" seems a little like overkill. Is there a better way?
Greg Buchholz
More information about the Python-list
mailing list