• API
• FAQ
• Tools
• Trends
• Archive
daily pastebin goal
25%
SHARE
TWEET

# Untitled

a guest Jan 4th, 2013 22 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. [e, ix] = min(X);
2.
3. min x = z \$ foldl' f (e,0,0) es
4.   where
5.     (e:es) = toList x
6.     f (a,ix,r) b = let ix' = ix+1 in if a < b then (a,ix',r) else (b,ix',ix')
7.     z (a,ix,r) = (a,r)
8.
9. min x = traverse x to0D min
10.   where
11.     to0D (Z:.i) = Z
12.     min f (Z) = ??? -- how to get elements for comparison?
13.
14. [f (Z:.i) | i <- [1..n]], where n = ((Z:.i) -> i) \$ extent x
15.
16. import Data.Array.Repa
17.
18. indexed arr = traverse arr id (src idx@(Z :. i) -> (src idx, i))
19.
20. minimize arr = foldP f h t
21.   where
22.     (Z :. n) = extent arr
23.     arr' = indexed arr
24.     h = arr' ! (Z :. 0)
25.     t = extract (Z :. 1) (Z :. (n-1)) arr'
26.     f min@(valMin, iMin) x@(val, i) | val < valMin = x
27.                                     | otherwise = min
28.
29. {-# LANGUAGE TypeOperators, FlexibleContexts #-}
30. import Data.Array.Repa
31.
32. idx :: Int -> (Z :. Int)
33. idx n = Z :. n
34.
35. (//) :: Source U e => Array U (Z :. Int) e -> Int -> e
36. (//) v n = v ! (idx n)
37.
38. min' n v = m 0
39.   where m p | p == n - 1 = (v // p, p)
40.             | True       = if x < x' then (x, p) else (x', p')
41.                            where (x', p') = m (p + 1)
42.                                  x = v // p
43.
44. v2 :: Array U (Z :. Int) Int
45. v2 = fromListUnboxed (idx 6) [7, 2, 9, 3, 0, 5]
46.
47. example = min' 6 v2
48. -- OUT: (0, 4)
RAW Paste Data
Top