Sample arbitrary amount of numbers from Haskell list -


i'm starting learn haskell ruby background. i'm looking able arbitrary number of items list:

sample [1,2,3,4,5,6,7,8,9,10] => 7 sample 3 [1,2,3,4,5,6,7,8,9,10] => [4,2,9] 

this available in ruby , i'm hoping same functionality. haven't been able find after googling, figured ask here. available or function have implement myself? thanks!

based on code @ http://ruby-doc.org/core-2.0/array.html sample, chooses n random indices array, came following:

import system.random import data.list import control.applicative  sample1 xs =   let l = length xs - 1   idx <- randomrio (0, l)   return $ xs !! idx  sample 0 xs = return [] sample n xs =   let l = min n (length xs)   val <- sample1 xs   (:) <$> (pure val) <*> (sample (l-1) (delete val xs)) 

alternatively, use control.monad instead of control.applicative , liftm2 (:) (return val) (sample (ct-1) (delete val xs))

using delete incur eq constraint on type of list elements, though, have split/merge @ index if needed avoid that.


Comments

Popular posts from this blog

android - Invite new users for my app using app notifications facebook -

Need help in packaging app using TideSDK on Windows -

html - CSS: Combine Texture and Color -