Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further changes to p23 #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dominikmoehrle
Copy link

Now compatible with p25. Before, it was not possible to enter a whole list as an argument.

Now compatible with p25. Before, it was not possible to enter a whole list as an argument.
aux (min n len) [] list len
let rec rand_select list n =
if n > 0 then let i = random ((List.length list)) in
if n < 0 || n > (List.length list) then raise Not_found else (List.nth list i) :: rand_select (remove_at i list) (n-1) (*rand_select from exercise 20*)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting: this line is too wide. The n < 0 test can be dropped (we are in the n > 0 case of the condition on line 7), and it would probably be possible to run the n > List.length list check exactly once (to avoid re-computing the length of the list twice on each iteration). In the end, we would have:

let rand_select list n = 
    let length = List.length list in 
    let rec extract n length list = 
        if n == 0 then [] else
            let i = random length in
            List.nth list i :: aux (n-1) (length - 1) (remove_at i list) 
    in
    if n < 0 || n > length then raise Not_found else aux n length list

Also, the comment probably refers to remove_at, not rand_select.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants