# Re: Narrowing vs. rewriting

Date: Thu, 03 Jul 1997 12:52:04 -0400

I don't find either Sergio's and Harold's examples very convincing,
as both are easy to program in Haskell. I'm sure there are examples
of programs that are easy to write in Curry but not so easy to write in

1. Sergio's example:

permute [] = []
permute (x:xs) | u++v == permute xs = u++[x]++v

The same in Haskell (returns a list of all possible permutations):

permute [] = [[]]
permute (x:xs) = [ take i ys ++ [x] ++ drop i ys
| ys <- permute xs, i <- [0..length ys] ]

This is slightly longer, but makes explicit the order in which the
permutations are examined. The functions (++), take, drop, and length
are part of the standard library.

2. Harold's example:

pairlists([],[]) = []
pairlists([X|L],[Y|M]) = [[X,Y]|pairlists(L,M)]

numbered([],N).
numbered([[X,N]|R],N) :- numbered(R,+(N,1)).

before([X1,Y1],[X2,Y2]) :- string<(X1,X2).

serialise(L) = R if numbered(qsort[before](pairlists(L,R)),1)

before (x1,y1) (x2,y2) = x1 <= x2
serialise l = map fst (sortBy before (zip (l, [1..])))

The functions (<=), map, fst, sortBy, and zip are part of the standard
library.

The library functions can be found in: