(* CIS 120 Lecture 3. Demonstrates basic list operations. *) ;; open Assert (* Collection of songs by Lady Gaga. *) let gaga_songs : string list = [ "Bad Romance"; "Alejandro"; "Monster"; "Speechless"; "Dance in the Dark"; "Telephone"; "So Happy I Could Die"; "Teeth"; "Just Dance"; "Love Game"; "Paparazzi"; "Beautiful, Dirty, Rich"; "Eh, Eh (Nothing Else I Can Say)"; "Poker Face"; "The Fame"; "Money Honey"; "Again Again"; "Boys Boys Boys"; "Brown Eyes"; "Summerboy" ] (* Some sample playlists *) let pl1 : string list = ["Bad Romance" ; "Nightswimming" ; "Telephone" ; "Everybody Hurts" ] let pl2 : string list = [ "Losing My Religion"; "Man on the Moon"; "Belong" ] let pl3 : string list = [] (* a playlist with no songs. *) let pl4 : string list = [ "Bad Romance"; "Goldberg Variations"; "Nightswimming"; "Summerboy"; "Telephone"; "Everybody Hurts"; "Bach's Prelude 19, A Major"; "Bach's Invention 13"; "Again Again"; ] (* Number of songs *) let rec number_of_songs (pl : string list) : int = begin match pl with | [] -> 0 | song :: pl1 -> 1 + number_of_songs pl1 end let test () : bool = (number_of_songs pl1) = 4 ;; run_test "number_of_songs pl1" test let test () : bool = (number_of_songs pl2) = 3 ;; run_test "number_of_songs pl2" test let test () : bool = (number_of_songs pl3) = 0 ;; run_test "number_of_songs pl3" test let test () : bool = (number_of_songs gaga_songs) = 20 ;; run_test "number_of_songs gaga_songs" test (* Contains *) let rec contains (pl : string list) (song : string) : bool = begin match pl with | [] -> false | song1 :: pl1 -> if song1 = song then true else contains pl1 song end let test () : bool = (contains pl1 "Telephone") = true ;; run_test "contains pl1 Telephone" test let test () : bool = (contains pl2 "Telephone") = false ;; run_test "contains pl2 Telephone" test let test () : bool = (contains pl3 "Telephone") = false ;; run_test "contains pl3 Telephone" test let test () : bool = (contains gaga_songs "Telephone") = true ;; run_test "contains gaga_songs Telephone" test (* number of gaga_songs *) let rec number_of_gaga_songs (pl : string list) : int = failwith "number_of_gaga_songs: unimplemented" let test () : bool = (number_of_gaga_songs pl1) = 2 ;; run_test "number_of_gaga_songs pl1" test let test () : bool = (number_of_gaga_songs pl2) = 0 ;; run_test "number_of_gaga_songs pl2" test let test () : bool = (number_of_gaga_songs pl3) = 0 ;; run_test "number_of_gaga_songs pl3" test (* all_gaga_songs *) let rec all_gaga_songs (pl : string list) : string list = failwith "all_gaga_songs: unimplemented" let test () : bool = (all_gaga_songs pl1) = [ "Bad Romance"; "Telephone" ] ;; run_test "all_gaga_songs pl1" test let test () : bool = (all_gaga_songs pl2) = [] ;; run_test "all_gaga_songs pl2" test let test () : bool = (all_gaga_songs pl3) = [] ;; run_test "all_gaga_songs pl3" test ;; print_endline "gaga.ml: end of file."