Changeset 10246 for src/Pugs/Prim/List.hs
- Timestamp:
- 05/09/06 16:17:14 (3 years ago)
- svk:copy_cache_prev:
- 13387
- Files:
-
- 1 modified
-
src/Pugs/Prim/List.hs (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Pugs/Prim/List.hs
r7591 r10246 4 4 op0Zip, op1Pick, op1Sum, 5 5 op1Min, op1Max, op1Uniq, 6 op2 FoldL, op2Fold, op2Grep, op2Map, op2Join,6 op2ReduceL, op2Reduce, op2Grep, op2Map, op2Join, 7 7 sortByM, 8 8 op1HyperPrefix, op1HyperPostfix, op2Hyper, … … 156 156 if cond then return . VBool $ cond else elemByM eq y xs 157 157 158 op2 FoldL :: Val -> Val -> Eval Val159 op2 FoldL sub@(VCode _) list = op2FoldL list sub160 op2 FoldL list sub = do158 op2ReduceL :: Val -> Val -> Eval Val 159 op2ReduceL sub@(VCode _) list = op2ReduceL list sub 160 op2ReduceL list sub = do 161 161 code <- fromVal sub 162 op2 Foldlist $ VCode code{ subAssoc = "left" }163 164 op2 Fold:: Val -> Val -> Eval Val165 op2 Fold sub@(VCode _) list = op2Foldlist sub166 op2 Foldlist sub = do162 op2Reduce list $ VCode code{ subAssoc = "left" } 163 164 op2Reduce :: Val -> Val -> Eval Val 165 op2Reduce sub@(VCode _) list = op2Reduce list sub 166 op2Reduce list sub = do 167 167 code <- fromVal sub 168 168 args <- fromVal list 169 cxt <- asks envContext 170 let (reduceM, reduceMn) = getReduceFuncs cxt 169 171 let arity = length $ subParams code 170 172 if arity < 2 then fail "Cannot reduce() using a unary or nullary function." else do … … 182 184 "right" -> do 183 185 let args' = reverse args 184 foldMn args' n (doFold . reverse)185 "chain" -> if arity /= 2 186 reduceMn args' n (doFold . reverse) 187 "chain" -> if arity /= 2 -- FIXME: incorrect for scans 186 188 then fail 187 189 "When reducing using a chain-associative sub,\nthe sub must take exactly two arguments." … … 192 194 VBool False -> esc val 193 195 _ -> return y 194 foldM doFold' (head args) (tail args)196 reduceM doFold' (head args) (tail args) 195 197 return $ VBool True 196 198 "non" -> fail $ "Cannot reduce over non-associativity" 197 _ -> foldMn args n doFold -- "left", "pre"199 _ -> reduceMn args n doFold -- "left", "pre" 198 200 where 199 201 -- This is a generalized foldM. … … 202 204 foldMn :: [Val] -> Int -> ([Val] -> Eval Val) -> Eval Val 203 205 foldMn list n f = foldM (\a b -> f (a:b)) (head list) $ list2LoL n $ drop 1 list 206 -- Scan version of foldMn. 207 scanMn :: [Val] -> Int -> ([Val] -> Eval Val) -> Eval Val 208 scanMn list n f = scanM (\a b -> f (a:b)) (head list) $ list2LoL n $ drop 1 list 209 -- The Prelude defines foldM but not scanM. 210 scanM :: (Val -> b -> Eval Val) -> Val -> [b] -> Eval Val 211 scanM f q ls = case ls of 212 [] -> return $ VList [q] 213 x:xs -> do 214 fqx <- f q x 215 rest <- fromVal =<< scanM f fqx xs 216 return $ VList (q:rest) 217 getReduceFuncs cxt = case cxt of 218 CxtSlurpy _ -> (scanM, scanMn) 219 _ -> (foldM, foldMn) 204 220 205 221 op2Grep :: Val -> Val -> Eval Val
