Show
Ignore:
Timestamp:
12/19/06 00:09:35 (2 years ago)
Author:
luqui
Message:

Implemented @list.pick(num).

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/Pugs/Prim/List.hs

    r14478 r14925  
    44    op0Zip, op0Cat, op0Each, op0RoundRobin, op1Pick, op1Sum, 
    55    op1Min, op1Max, op1Uniq, 
     6    op2Pick, 
    67    op2ReduceL, op2Reduce, op2Grep, op2Map, op2Join, 
    78    sortByM, 
     
    5758    else return undef 
    5859op1Pick v = retError "pick not defined" v 
     60 
     61shuffle :: [a] -> Eval [a] 
     62shuffle [] = return [] 
     63shuffle xs = do 
     64    -- pick the first element 
     65    first <- liftIO $ randomRIO (0 :: Int, length xs - 1) 
     66    rest <- shuffle $ take first xs ++ drop (first+1) xs 
     67    return $ head (drop first xs) : rest 
     68 
     69op2Pick :: Val -> Val -> Eval Val 
     70op2Pick (VRef r) num = do 
     71    ref <- readRef r 
     72    op2Pick ref num 
     73op2Pick (VList xs) (VInt num) = do 
     74    shuffled <- shuffle xs 
     75    return $ VList $ take (fromInteger num) shuffled 
     76op2Pick r _ = retError "pick not defined" r 
    5977 
    6078op1Sum :: Val -> Eval Val