| 183 | | _ -> foldM doFold (head args) (tail args) -- "left", "pre" |
| | 193 | _ -> foldMn args n doFold -- "left", "pre" |
| | 194 | where |
| | 195 | -- This is a generalized foldM. |
| | 196 | -- It takes an input list (from which the first elem will be used as start |
| | 197 | -- value), the number of additional arguments, and a reducing function. |
| | 198 | foldMn :: [Val] -> Int -> ([Val] -> Eval Val) -> Eval Val |
| | 199 | foldMn list n f = foldM (\a b -> f (a:b)) (head list) $ list2LoL n $ drop 1 list |
| 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." |
| | 230 | mapMn' :: [[Val]] -> ([Val] -> Eval [Val]) -> Eval [Val] |
| | 231 | mapMn' (x:xs) f = liftM2 (++) (f x) (mapMn' xs f) |
| | 232 | mapMn' [] _ = return [] |
| | 233 | |
| | 234 | -- | Takes an int and a list and returns a LoL. |
| | 235 | -- Ex.: |
| | 236 | -- > list2LoL 3 [1,2,3,4,5] = [[1,2,3],[4,5,undef]] |
| | 237 | list2LoL :: Int -> [Val] -> [[Val]] |
| | 238 | list2LoL n list |
| | 239 | | n == 0 = fail "Cannot map() using a nullary function." |
| | 240 | -- If the list has exactly n elements, we've finished our work. |
| | 241 | | length list == n = [list] |
| | 242 | -- If the list is empty, we're done, too. |
| | 243 | | length list == 0 = [] |
| | 244 | -- But if the list contains more elems than we need, we process the |
| | 245 | -- first n ones and the rest separately. |
| | 246 | | length list > n = (list2LoL n $ take n list) ++ (list2LoL n $ drop n list) |
| | 247 | -- And if the list contains less elems than we need, we pad with undefs. |
| | 248 | | length list < n = list2LoL n $ list ++ [undef :: Val] |
| | 249 | | otherwise = fail "Invalid arguments to internal function list2LoL passed." |