5 newtype XPtr a = XPtr Addr
6 unXPtr (XPtr (A# x)) = x
11 foreign export dynamic
12 mkComparator :: (XPtr Int -> XPtr Int -> IO CInt)
13 -> IO (XPtr (XPtr Int -> XPtr Int -> IO CInt))
16 qsort :: Ptr Int -> CSize -> CSize -> XPtr (XPtr Int -> XPtr Int -> IO CInt)
19 compareInts :: XPtr Int -> XPtr Int -> IO CInt
20 compareInts a1 a2 = do
21 i1 <- peek (Ptr (unXPtr a1))
22 i2 <- peek (Ptr (unXPtr a2))
23 return (fromIntegral (i1 - i2 :: Int))
27 let values = [ 12, 56, 90, 34, 78 ] :: [Int]
30 zipWithM_ (pokeElemOff buf) [ 0 .. ] values
31 c <- mkComparator compareInts
32 qsort buf (fromIntegral n) (fromIntegral (sizeOf (head values))) c
33 mapM (peekElemOff buf) [ 0 .. n-1 ] >>= (print :: [Int] -> IO ())