Library report examples
Simon Peyton-Jones
simonpj@microsoft.com
Tue, 27 Nov 2001 02:28:45 -0800
| The library report defines
|=20
| -- Diagonal of a square matrix
| diag :: (Ix a) =3D> Array (a,a) b -> Array a b
| diag x =3D ixmap (l,u) (\i->(i,i)) x
| where ((l,l'),(u,u')) | l =3D=3D l' && u =3D=3D u' =3D bounds x
And that is indeed a stupid definition. It's like saying
x =3D if x>2 then 1 else 10
(the guard is really just an 'if'.) So of course l, u etc are all
bottom. =20
The guard needs to be on diag itself
diag x | l=3D=3Dl' && u=3D=3Du' =3D ixmap ...as before
where
((l,l'),(u,u')) =3D bounds x
thanks for pointing this out
| I am also curious why, for example,
|=20
| row :: (Ix a, Ix b) =3D> a -> Array (a,b) c -> Array b c
| row i x =3D ixmap (l',u') (\j->(i,j)) x where ((l,l'),(u,u'))=20
| =3D bounds x
|=20
| isn't written as
|=20
| row :: (Ix a, Ix b) =3D> a -> Array (a,b) c -> Array b c
| row i x =3D ixmap (l,u) (\j->(i,j)) x where ((_,l),(_,u)) =3D bounds =
x
| ~~~ ~~~ ~~~
you can write it either way
S