Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let l = [ 0; 1; 3; -1; -2; -4; 10 ];;
- let rec remove_negative = function
- | [ ] -> [ ]
- | head :: tail ->
- if head < 0 then
- remove_negative tail
- else
- head :: remove_negative tail
- ;;
- (* And here is the function I would like to be able to write: *)
- let rec remove_negative l = match l with
- | [ ] -> [ ]
- | head :: tail ->
- if head < 0 then
- (* remove all the items at the beginning that are negative *)
- remove_negative tail
- else
- (* when you get one non-negative item *)
- let loop prev curr = match curr with
- | [ ] -> l
- | head :: tail ->
- (* make the list "skip" all items that are negative *)
- if head < 0 then
- prev.tail <- tail
- else ( );
- loop prev tail
- in
- loop l tail
- (* *)
- let rec print_list = function
- | [ ] -> ( )
- | head :: tail ->
- print_int head;
- print_newline ( );
- print_list tail
- ;;
- print_list( remove_negative l );;
Advertisement
Add Comment
Please, Sign In to add comment