Explanation of macros; Haskell macros
Pascal Costanza
costanza at web.de
Fri Oct 31 17:11:40 EST 2003
Dirk Thierbach wrote:
> Since the overhead of evaluating it at runtime is minimal, especially
> with lazyness, that's exactly the situation where it is natural to use
> a HOF instead of a macro.
OK, nice solution.
> I didn't go through the two proposed Lisp solutions in detail, but here's
> a HOF in Haskell that does the same. First a general 'cond' with an
> explicit default case, then 'econd' based on cond with an error as
> default case:
>
> cond :: a -> [(Bool, a)] -> a
> cond def [] = def
> cond def ((True,x):_) = x
> cond def ((False,_):xs) = econd xs
^^^^^^^^
I guess this is a typo - it should be "cond xs", right?
> econd :: [(Bool, a)] -> a
> econd = cond (error "Default econd case")
Hmm, what if I had wanted to add a default check in front of each cond,
instead of at the end?
Also, Coby's version prints a useful error message that mentions all
conditions ("could not satisfy..."):
> CL-USER 91 > (econd
> ((= 3 4) "foo")
> ((= 4 4) "bar"))
> "bar"
>
> CL-USER 92 > (econd
> ((= 3 4) "foo")
> ((= 4 5) "bar"))
>
> Error: fell through ECOND form. could not satisfy any of the following:
> (= 3 4)
> (= 4 5)
Would it be possible to add such a message with your proposed technique?
Pascal
More information about the Python-list
mailing list