{-
    COMS 4995: Parallel Functional Programming, Fall 2025
    Quiz 8: Repa

    1. Close all devices (phone/laptop, etc). Make sure you have an index card to write on, and a pen/pencil.

    2. PLEASE WRITE YOUR NAME AND UNI AS LEGIBLY AS POSSIBLE

    3. Read the quiz question below, and write your answer on your index card. CIRCLE YOUR ANSWER.

    4. Turn in your card at the table in the front of the room.
-}

import Data.Array.Repa
import Data.Functor.Identity

a :: Array D DIM2 Int
a = fromFunction (Z :. 10 :. 10) (\(Z :. i :. j) -> j * 10 + i)

repaMap :: (Source r t, Shape sh) => (t -> a) -> Array r sh t 
           -> Array D sh a
repaMap f arr = fromFunction (extent arr) (\i -> f (arr ! i))

parResult :: Array U DIM2 Int
parResult = runIdentity $ computeP $ repaMap (`mod` 10) a

-- QUESTION:
-- parResult computes an operation over each element of `a` in parallel.
-- What is `parResult ! (Z :. 6 :. 3)`?

-- ANSWER:
-- 6

-- EXPLANATION:
-- a[i, j] = (j * 10 + i) by the first line.
-- So a[6,3] = 36.
-- parResult computes each element `mod` 10 (via a map).
-- So parResult[6,3] = 36 % 10 = 6