[Haskell-cafe] General function to count list elements?
Richard O'Keefe
ok at cs.otago.ac.nz
Sun Apr 19 23:26:19 EDT 2009
On 19 Apr 2009, at 4:27 am, michael rice wrote:
> I know functions can be compared in Scheme
>
> Welcome to DrScheme, version 4.1 [3m].
> Language: Swindle; memory limit: 128 megabytes.
> > (equal? equal? equal?)
> #t
> >
>
> but apparently not in Haskell
I have a copy of R6RS somewhere, but I'll refer to R5RS.
Section 6.1 says there are three equality predicates:
eq?
eqv?
equal?
(eqv? obj1 obj2) returns #t if "obj1 nd obj2 are procedures
whose location-tags are equal", it returns #f if they "are
procedures that would behave differently ... for some
arguments." Section 4.1.4 says "Each procedure created as
the result of evaluating a lambda expression is (conceptually)
tagged with a storage location in order to make eqv? and eq?
work on procedures."
What this boils down to is that
(let ((F (lambda (X) (list X X))))
(eqv? F F))
is defined to answer true and
(let ((F (lambda (X) (list X X)))
(G (lambda (X) (cons X X))))
(eqv? F G))
is defined to answer false. But the Scheme reports are
very very careful to leave
(let ((F (lambda (X) (list X X)))
(G (lambda (X) (list X X))))
(eqv? F G))
*UN*defined except that it is some Boolean value.
Applied to procedures, eq? and equal? are the same as eqv?.
Scheme equality of procedures is not the same thing as
mathematical equality of functions. Two mathematically
equal functions may or may not compare equal in Scheme.
And Scheme implementations may differ. For example,
(let ((F (lambda (X) (list X X)))
(G (lambda (X) (list X X))))
(eqv? F G))
*might* be optimised to #t, but then again, it *might*
be optimised to #f, and both answers are "right" by R5RS.
Haskell tries to be a little more defined than that (:-).
More information about the Haskell-Cafe
mailing list