| 87 | | sub <- fromVal subVal |
| 88 | | evl <- asks envEval |
| 89 | | -- Here we execute the user's sub |
| 90 | | foldM (\a b -> do |
| 91 | | rv <- local (\e -> e{ envContext = cxtItem "Int" }) $ do |
| 92 | | evl (App (Val sub) Nothing [Val a, Val b]) |
| 93 | | int <- fromVal rv |
| 94 | | -- If the return value from the sub was |
| 95 | | -- -1 ==> a < b |
| 96 | | -- 0 ==> a == b |
| 97 | | -- +1 ==> a > b |
| 98 | | -- We call min_or_max so we can work for both min() and max(). |
| 99 | | return $ if min_or_max (int > (0::VInt)) then a else b) (head valList) valList |
| | 87 | sub <- fromVal subVal |
| | 88 | evl <- asks envEval |
| | 89 | -- Here we execute the user's sub |
| | 90 | foldM (\a b -> do |
| | 91 | rv <- local (\e -> e{ envContext = cxtItem "Int" }) $ do |
| | 92 | evl (App (Val sub) Nothing [Val a, Val b]) |
| | 93 | int <- fromVal rv |
| | 94 | -- If the return value from the sub was |
| | 95 | -- -1 ==> a < b |
| | 96 | -- 0 ==> a == b |
| | 97 | -- +1 ==> a > b |
| | 98 | -- We call min_or_max so we can work for both min() and max(). |
| | 99 | return $ if min_or_max (int > (0::VInt)) then a else b) (head valList) valList |
| 130 | | sub <- fromVal subVal |
| 131 | | evl <- asks envEval |
| 132 | | -- Here we execute the user's sub |
| 133 | | result <- nubByM (\a b -> do |
| 134 | | rv <- local (\e -> e{ envContext = cxtItem "Bool" }) $ do |
| 135 | | evl (App (Val sub) Nothing [Val a, Val b]) |
| 136 | | -- The sub returns either true or false. |
| 137 | | bool <- fromVal rv |
| 138 | | return . VBool $ bool) valList |
| 139 | | return . VList $ result |
| | 130 | sub <- fromVal subVal |
| | 131 | evl <- asks envEval |
| | 132 | -- Here we execute the user's sub |
| | 133 | result <- nubByM (\a b -> do |
| | 134 | rv <- local (\e -> e{ envContext = cxtItem "Bool" }) $ do |
| | 135 | evl (App (Val sub) Nothing [Val a, Val b]) |
| | 136 | -- The sub returns either true or false. |
| | 137 | bool <- fromVal rv |
| | 138 | return . VBool $ bool) valList |
| | 139 | return . VList $ result |
| 144 | | nubByM' [] _ = return [] |
| 145 | | nubByM' (y:ys) xs = do |
| 146 | | -- elemByM returns a Val, but we need a VBool, so we have to use fromVal. |
| 147 | | cond <- fromVal =<< elemByM eq y xs |
| 148 | | if cond then nubByM' ys xs else do |
| 149 | | result <- nubByM' ys (y:xs) |
| 150 | | return (y:result) |
| 151 | | elemByM :: (Val -> Val -> Eval Val) -> Val -> [Val] -> Eval Val |
| 152 | | elemByM _ _ [] = return . VBool $ False |
| 153 | | elemByM eq y (x:xs) = do |
| 154 | | cond <- fromVal =<< eq x y |
| 155 | | -- Same here (we need a VBool, not a Var). |
| 156 | | if cond then return . VBool $ cond else elemByM eq y xs |
| | 144 | nubByM' [] _ = return [] |
| | 145 | nubByM' (y:ys) xs = do |
| | 146 | -- elemByM returns a Val, but we need a VBool, so we have to use fromVal. |
| | 147 | cond <- fromVal =<< elemByM eq y xs |
| | 148 | if cond then nubByM' ys xs else do |
| | 149 | result <- nubByM' ys (y:xs) |
| | 150 | return (y:result) |
| | 151 | elemByM :: (Val -> Val -> Eval Val) -> Val -> [Val] -> Eval Val |
| | 152 | elemByM _ _ [] = return . VBool $ False |
| | 153 | elemByM eq y (x:xs) = do |
| | 154 | cond <- fromVal =<< eq x y |
| | 155 | -- Same here (we need a VBool, not a Var). |
| | 156 | if cond then return . VBool $ cond else elemByM eq y xs |