| 207 | | return $ VList $ concat vals |
| | 208 | return $ VList vals |
| | 209 | where |
| | 210 | -- Takes a list, an arity, and a function. |
| | 211 | mapMn :: [Val] -> Int -> ([Val] -> Eval [Val]) -> Eval [Val] |
| | 212 | mapMn list n f = mapMn' (l2lol n list) f |
| | 213 | -- Takes a LoL and a function and applies the function to the inputlist. |
| | 214 | mapMn' :: [[Val]] -> ([Val] -> Eval [Val]) -> Eval [Val] |
| | 215 | mapMn' (x:xs) f = liftM2 (++) (f x) (mapMn' xs f) |
| | 216 | mapMn' [] _ = return [] |
| | 217 | -- Takes a list and returns a LoL. |
| | 218 | -- Ex.: l2lol 3 [1,2,3,4,5] = [[1,2,3],[4,5,undef]] |
| | 219 | l2lol n list |
| | 220 | | n == 0 = fail "Cannot map() using a nullary function." |
| | 221 | -- If the list has exactly n elements, we've finished our work. |
| | 222 | | length list == n = [list] |
| | 223 | -- If the list is empty, we're done, too. |
| | 224 | | length list == 0 = [] |
| | 225 | -- But if the list contains more elems than we need, we process the |
| | 226 | -- first n ones and the rest separately. |
| | 227 | | length list > n = (l2lol n $ take n list) ++ (l2lol n $ drop n list) |
| | 228 | -- And if the list contains less elems than we need, we pad with undefs. |
| | 229 | | length list < n = l2lol n $ list ++ [undef :: Val] |
| | 230 | | otherwise = fail "Invalid arguments to internal function l2lol passed." |