首页 > 其他分享 >Cornell cs3110 - Chapter3 Exercises

Cornell cs3110 - Chapter3 Exercises

时间:2024-10-02 19:23:51浏览次数:1  
标签:Leaf Cornell Chapter3 else h2 let match Exercises Exercise

(* Exercise: list expressions *)
let list1 = [1; 2; 3; 4; 5];;
let list2 = 1 :: 2 :: 3 :: 4 :: 5 :: [];;
let list3 = [1] @ [2; 3; 4;] @ [5];;

(* Exercise: product *)
let rec product l =
  match l with 
  | [] -> 1
  | h :: t -> h * product t;;

(* Exercise: concat *)
let rec concat l =
  match l with 
  | [] -> ""
  | h :: t -> h ^ concat t;;

(* Exercise: product test *)
let product_test = product [1; 2; 3; 4; 5] = 120;;

(* Exercise: concat test *)
let concat_test = concat ["a"; "b"; "c"; "d"; "e"] = "abcde";;

(* Exercise: patterns *)
let pattern_match_1 = function
  | h :: t -> if h = "bigred" then true else false 
  | _ -> false;;

let rec pattern_match_2 l depth = 
  match l with
  | [] -> if depth = 2 || depth = 4 then true else false 
  | h :: t -> pattern_match_2 t (depth + 1);;

let pattern_match_3 = function 
  | h1 :: h2 :: t -> if h1 = h2 then true else false
  | _ -> false;;

(* Exercise: library *)
let library_func_1 l = 
  if List.length l < 5 then 0 
  else List.nth l 4;;

let library_func_2 l =
  List.sort compare l |> List.rev;;

(* Exercise: library test *)
let library_test_1 = library_func_1 [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] = 5;;

let library_test_2 = library_func_2 [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] = [10; 9; 8; 7; 6; 5; 4; 3; 2; 1];;

(* Exercise: library puzzle *)
let library_puzzle_1 l =
  if List.length l = 0 then None
  else Some ((List.length l - 1) |> List.nth l);;

let any_zeros l =
  List.exists (fun x -> x = 0) l;;

(* Exercise: take drop *)
let rec take n l = 
  match l with 
  | [] -> []
  | h :: t -> if n = 0 then [] else h :: take (n - 1) t;;

let rec drop n l = 
  match l with 
  | [] -> []
  | h :: t -> if n = 0 then l else drop (n - 1) t;;

(* Exercise take drop tail *)
let rec take n l1 l2 =
  match l1 with
  | [] -> []
  | h :: t -> if n = 0 then l2 else take (n - 1) t (l2 @ [h]);;

(* Exercise: unimodal *)
let rec is_unimodal l direction = 
  match l with 
  | [] -> true
  | h :: [] -> true
  | h1 :: h2 :: t -> 
    if direction = 0 then 
      if h1 < h2 then is_unimodal (h2 :: t) 1
      else if h1 > h2 then is_unimodal (h2 :: t) 2
      else false
    else if direction = 1 then 
      if h1 < h2 then is_unimodal (h2 :: t) 1
      else if h1 > h2 then is_unimodal (h2 :: t) 0
      else is_unimodal (h2 :: t) 0
    else 
      if h1 < h2 then false
      else if h1 > h2 then is_unimodal (h2 :: t) 2
      else is_unimodal (h2 :: t) 0;;

(* Exercise: powerest *)
let rec powerest = function 
  | [] -> [[]]
  | h :: t -> (powerest t) @ (List.map (fun x -> h :: x) (powerest t));;

(* Exercise: print int list rec*)
let rec print_int_list l = 
  match l with 
  | [] -> ()
  | h :: t -> print_int h; print_string "\n"; print_int_list t;;

(* Exercise: print int list iter *)
let print_int_list_iter l = 
  List.iter (fun x -> print_int x; print_string "\n") l;;

(* Exercise: student *)
type student = {first_name: string; last_name: string; gpa: float};;

let stu1 : student = {first_name = "John"; last_name = "Doe"; gpa = 3.5};;

let get_name s = s.first_name ^ " " ^ s.last_name;;

let get_record s =  s.first_name ^ " " ^ s.last_name ^ " " ^ string_of_float s.gpa;;

(* Exercise: pokerrecord *)
type pokertype = Normal | Fire | Water;;

type pokerman = {
  name: string;
  hp: int;
  ptype: pokertype
};;

let charizard = {name = "Charizard"; hp = 78; ptype = Fire};;

let squirtle = {name = "Squirtle"; hp = 44; ptype = Water};;

(* Exercise: safe hd and tl *)
let safe_hd l = 
  match l with 
  | [] -> None
  | h :: t -> Some h;;

let safe_tl l =
  match l with 
  | [] -> None
  | h :: t -> Some t;;

(* Exercise: pokefun *)
let rec max_hp = function 
  | [] -> None 
  | h :: [] -> Some h.hp
  | h :: t -> if h.hp > (max_hp t |> Option.get) then Some h.hp else max_hp t;;

(* Exercise: date before *)
type date = int * int * int;;

let is_before d1 d2 = 
  match d1, d2 with 
  | (y1, m1, d1), (y2, m2, d2) -> 
    if y1 < y2 then true
    else if y1 = y2 then 
      if m1 < m2 then true
      else if m1 = m2 then 
        if d1 < d2 then true
        else false
      else false
    else false;;

(* Exercise: earliest date *)
let rec earliest = function 
  | [] -> None
  | h :: [] -> Some h
  | h :: t -> if is_before h (earliest t |> Option.get) then Some h else earliest t;;

(* Exercise: assoc list *)
let insert k v lst = (k, v) :: lst;;

let assoc_list = [] |> insert "a" 1 |> insert "b" 2 |> insert "c" 3;;

(* Exercise: quadrant *)

type quad = I | II | III | IV;;

type sign = Neg | Zero | Pos;;

let sign (x : int) : sign = 
  if x < 0 then Neg
  else if x = 0 then Zero
  else Pos;;

let quadrant : int * int -> quad option = fun (x, y) ->
  match sign x, sign y with
  | Pos, Pos -> Some I
  | Neg, Pos -> Some II
  | Neg, Neg -> Some III
  | Pos, Neg -> Some IV
  | _, _ -> None;;

(* Exercise: quadrant when *)
let quadrant_when : int * int -> quad option = function
  | (x, y) when x > 0 && y > 0 -> Some I
  | (x, y) when x < 0 && y > 0 -> Some II
  | (x, y) when x < 0 && y < 0 -> Some III
  | (x, y) when x > 0 && y < 0 -> Some IV
  | _ -> None;;

(* Exercise: depth *)
type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree;;

let rec depth = function 
  | Leaf -> 0
  | Node (_, l, r) -> max (depth l) (depth r) + 1;;


(* Exercise: shape *)
let rec shape t1 t2 = 
  match t1, t2 with 
  | Leaf, Leaf -> true 
  | Node (_, l1, r1), Node (_, l2, r2) -> shape l1 l2 && shape r1 r2
  | _, _ -> false;;

(* Exercise: list max exn *)
let rec list_max_exn = function 
  | [] -> raise (Failure "empty list")
  | h :: [] -> h
  | h :: t -> max h (list_max_exn t);;

(* Exercise: is_bst *)
let rec is_bst = function 
  | Leaf -> true
  | Node (v, Leaf, Leaf) -> true
  | Node (v, Leaf, Node (rv, _, _)) -> v < rv && is_bst (Node (rv, Leaf, Leaf))
  | Node (v, Node (lv, _, _), Leaf) -> v > lv && is_bst (Node (lv, Leaf, Leaf))
  | Node (v, Node (lv, _, _), Node (rv, _, _)) -> v > lv && v < rv && is_bst (Node (lv, Leaf, Leaf)) && is_bst (Node (rv, Leaf, Leaf))
  | _, _ -> false;;

标签:Leaf,Cornell,Chapter3,else,h2,let,match,Exercises,Exercise
From: https://www.cnblogs.com/sysss-blogs/p/18444999

相关文章

  • Cornell University's Textbook Reading Systems(教科书阅读系统-康奈尔大学)
    TextbookReadingSystemsTextbookreadingsystemshelpyouinteractwiththeinformationintextbookssothatyoucanbetterinternalizeandlearnSQ3RTheSQ3Risasystematicmethoddesignedforstudyingatextbook.DevelopedbyFrancisP.Robinson,......
  • 论文翻译:Evaluating Reading Comprehension Exercises Generated by LLMs: A Showcase
    EvaluatingReadingComprehensionExercisesGeneratedbyLLMs:AShowcaseofChatGPTinEducationApplicationshttps://aclanthology.org/2023.bea-1.52.pdfhttps://aclanthology.org/2023.bea-1.52/文章目录由大型语言模型(LLMs)生成的阅读理解练习评估:教育应用......
  • 论文阅读:Evaluating Reading Comprehension Exercises Generated by LLMs: A Showcase
    EvaluatingReadingComprehensionExercisesGeneratedbyLLMs:AShowcaseofChatGPTinEducationApplicationshttps://aclanthology.org/2023.bea-1.52.pdfhttps://aclanthology.org/2023.bea-1.52/这篇论文探讨了如何利用预训练的大型语言模型(LLMs),特别是OpenAI的......
  • chapter3------保护模式之特权级转移
    特权级特权级总共有4个级别,数字越小表示的特权级越大CPL-CurrentPrivilegeLevel(当前特权级):用于指示处理器当前运行的特权级别DPL-DescriptorPrivilegeLevel(描述符特权级):用于指示段描述符或门描述符的特权级别。每个段描述符或门描述符都有一个DPL字段,它决定了访问该......
  • chapter3------保护模式之从保护模式跳转回实模式
    1、准备合适的段选择子在保护模式下,段寄存器存储的是段选择子,而不是实际的段地址。要切换到实模式,需要准备一个适当的段选择子,它指向实模式下要访问的代码段。示例程序:LABEL_DESC_NORMAL:Descriptor0,0ffffh,DA_DRW;Normal描述符SelectorNormaleq......
  • chapter3------保护模式之初始化GDT
    实模式运行于16位的CPU环境下:16位的寄存器16位的数据总线20位的地址总线,以及1MB的寻址能力(2^20B)一个地址由段和偏移两部分组成,物理地址=段值x16+偏移(段值和偏移都是16位,段值左移四位最后计算出来的地址才是20位)保护模式运行与32位的CPU环境下32位的寄存器32位......
  • Exercises
    ###Auto自动化变量自动存储类别是默认的存储类别,通常用于在”函数内部定义的局部变量“。这些变量会在程序执行到其定义的代码块时对应的栈空间被创建,函数执行完毕后变量对应栈空间会自动销毁。示例:intmain()//宿主{autointdata;//寄生虫autointdata;局......
  • Supplementary Exercises [The Loons]
    SupplementaryExercises [TheLoons] I.TranslatethefollowingintoChinese.选择Reynolds来主持这个节目很奇怪。1. Reynoldswasan odd choice to host theshow. 2. Shemovedfromplacetoplacewhereshecouldfindtheodd bitofwork. 3. O......
  • chapter3-排序和查找2
    2.基础查找所谓查找,就是在查找空间中找寻符合要求的解的过程。查找方法有多种,下面简单介绍3种。不同的策略对查找的效率和结果有不同的影响。2.1线性查找从首元素开始,遍历整个序列,直到找到目标元素,则结束算法;或者遍历完序列还没有匹配,则查找失败结束算法。时间复杂度为O(n)。......
  • chapter3-排序和查找
    1.排序排序就是把一组无序的数据变成有序的过程。对于机试而言,直接使用C++封装好的sort函数就可以了。sort函数内部采用快速排序实现,因此非常高效。使用sort函数,需要引用#include<algorithm>头文件,sort(first_address,last_address,compare)有三个参数,first和last待排序序列的......