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

Cornell cs3110 - Chapter5 Exercises

时间:2024-10-03 19:44:32浏览次数:7  
标签:string val Cornell module Chapter5 let Exercises type Exercise

(* Exercise: complex synonym *)
module type ComplexSig = sig
  type complex
  val zero : complex 
  val add : complex -> complex -> complex 
end

(* Exercise: complex encapsulation *)
module Complex : ComplexSig = struct
  type complex = float * float
  let zero = (0.0, 0.0)
  let add = fun (a, b) (c, d) -> (a +. c, b +. d)
  (* 不实现其中任何一项都会造成 The value "..." is required but not provided *)
end

(* Exercise: big list queue *)
module type QueueSig = sig
  type 'a queue
  val empty : 'a queue
  val enqueue : 'a -> 'a queue -> 'a queue
  val dequeue : 'a queue -> 'a * 'a queue
end

module ListQueue : QueueSig = struct
  type 'a queue = 'a list
  let empty = []
  let enqueue x q = q @ [x] (* Linear time *)
  let dequeue q = match q with
    | [] -> raise (Failure "empty queue")
    | x::xs -> (x, xs) (* Constant time *)
end

(* Exercise: binary search tree map *)
(* 用二叉搜索树的概念实现一个类似于 Map 的支持键值对操作的数据结构 比如查询 插入 *)
module type BstMap = sig
  type ('k, 'v) bstmap
  val empty : ('k, 'v) bstmap
  val insert : 'k -> 'v -> ('k, 'v) bstmap -> ('k, 'v) bstmap
  val lookup : 'k -> ('k, 'v) bstmap -> 'v option
  val create : ('k * 'v) list -> ('k, 'v) bstmap
end

module BstMapImpl : BstMap = struct
  type ('k, 'v) bstmap = 
    | Leaf
    | Node of ('k * 'v) * ('k, 'v) bstmap * ('k, 'v) bstmap
  let empty = Leaf
  let rec insert k v = function
    | Leaf -> Node ((k, v), Leaf, Leaf)
    | Node ((k', v'), left, right) -> 
      if k = k' then Node ((k, v), left, right)
      else if k < k' then Node ((k', v'), insert k v left, right)
      else Node ((k', v'), left, insert k v right)
  let rec lookup k = function
    | Leaf -> None
    | Node ((k', v'), left, right) -> 
      if k = k' then Some v'
      else if k < k' then lookup k left
      else lookup k right
  let create lst = List.fold_left (fun acc (k, v) -> insert k v acc) empty lst
end 

(* Exercise: fraction *)
module type FractionSig = sig
  (* A fraction is a rational number p/q, where q != 0. *)
  type t
  (* [make n d] is n/d. Requires d != 0. *)
  val make : int -> int -> t
  val numerator : t -> int 
  val denumerator : t -> int 
  val to_string : t -> string 
  val to_float : t -> float 
  val add : t -> t -> t
  val mul : t -> t -> t
end

module Fraction : FractionSig = struct
  type t = int * int 
  let make n d = if d = 0 then raise (Failure "denumerator cannot be zero") else (n, d)
  let numerator f = fst f
  let denumerator f = snd f
  let to_string f = (numerator f |> string_of_int) ^ "/" ^ (denumerator f |> string_of_int)
  let to_float f = (float_of_int (numerator f)) /. (float_of_int (denumerator f))
  let add f1 f2 = make (numerator f1 * denumerator f2 + numerator f2 * denumerator f1) (denumerator f1 * denumerator f2)
  let mul f1 f2 = make (numerator f1 * numerator f2) (denumerator f1 * denumerator f2)
end

(* Exercise: fraction reduced *)
let rec gcd x y =
  if y = 0 then x else gcd y (x mod y)

let reduce f = 
  if fst f > 0 && snd f < 0 then 
    let g = gcd (abs (fst f)) (abs (snd f)) in
    (- fst f / g, - snd f / g)
  else if fst f < 0 && snd f < 0 then 
    let g = gcd (abs (fst f)) (abs (snd f)) in
    (fst f / g, snd f / g)
  else 
    let g = gcd (abs (fst f)) (abs (snd f)) in
    (fst f / g, snd f / g)

(* Applying these two functions in Fraction will help *)

(* Exercise: make char map *)
module CharMap = Map.Make(Char)
(* Typing it in utop, which will display all types and methods declared *)

(* Exercise: use char map *)
let char_map = CharMap.(empty |> add 'A' "Alpha" |> add 'B' "Beta")
let char_map_mem = CharMap.mem 'A' char_map
let char_map_bindings = CharMap.bindings char_map

(* Exercise: date order *)
type date = {month : int; day : int}

module Date = struct
  type t = date
  let compare d1 d2 = 
    if d1.month = d2.month then d1.day - d2.day
    else d1.month - d2.month
end

(* Exercise: calendar *)
module DateMap = Map.Make(Date)

type calendar = string DateMap.t

let calendar : calendar = DateMap.empty |> DateMap.add {month = 1; day = 1} "New Year" |> DateMap.add {month = 2; day = 14} "Valentine's Day"

let calendar_bindings = DateMap.bindings calendar

(* Exercise: print calendar *)

let print_calendar calendar = 
  DateMap.iter (fun date event -> Printf.printf "%d/%d: %s\n" date.month date.day event) calendar

(* Exercise: is for *)
let is_for : string CharMap.t -> string CharMap.t = fun m -> 
  CharMap.fold (fun k v acc -> CharMap.add k ("I am for " ^ v) acc) m CharMap.empty

(* Exercise: first after *)
let first_after : calendar -> Date.t -> string = fun calendar date -> DateMap.find_first (fun d -> Date.compare d date > 0) calendar |> snd

(* Exercise: first after *)

(* Exercise: sets *)
module CaseIntendSet = struct
  type t = string
  let compare x y = String.compare (String.lowercase_ascii x) (String.lowercase_ascii y)
end

module CaseIntendSet = Set.Make(CaseIntendSet)

(* Exercise: ToString *)
module type ToStringSig = sig
  type t 
  val to_string : t -> string
end

(* Exercise: Print *)
module Print (M : ToStringSig) = struct
  let print x = print_endline (M.to_string x)
end

(* Exercise: Print Int *)
module IntForPrint = struct
  type t = int 
  let to_string = string_of_int
end

module PrintInt = Print(IntForPrint)

标签:string,val,Cornell,module,Chapter5,let,Exercises,type,Exercise
From: https://www.cnblogs.com/sysss-blogs/p/18445925

相关文章

  • Cornell cs3110 - Chapter4 Exercises
    (*Exercise:mysteryoperator1*)let($)fx=fx;;(*使得函数的连续调用具有一部分右结合的特质square$2+2与square2+2的运行结果分别是16和6*)(*Exercise:repeat*)letrecrepeatfnx=matchnwith|0->x|_->repeatf(n-1)......
  • Cornell cs3110 - Chapter3 Exercises
    (*Exercise:listexpressions*)letlist1=[1;2;3;4;5];;letlist2=1::2::3::4::5::[];;letlist3=[1]@[2;3;4;]@[5];;(*Exercise:product*)letrecproductl=matchlwith|[]->1|h::t->h*productt;;(*......
  • Cornell University's Textbook Reading Systems(教科书阅读系统-康奈尔大学)
    TextbookReadingSystemsTextbookreadingsystemshelpyouinteractwiththeinformationintextbookssothatyoucanbetterinternalizeandlearnSQ3RTheSQ3Risasystematicmethoddesignedforstudyingatextbook.DevelopedbyFrancisP.Robinson,......
  • chapter5------编写主引导扇区代码
    主引导扇区(MainBootSector,MBR)什么是主引导扇区:处理器加电或者复位之后(简单来说就是启动计算机),如果硬盘是首选的启动设备,那么ROM-BIOS(基本输入输出系统)将试图读取硬盘的0面0道1扇区(简单来说就是第一个扇区),这就是主引导扇区主引导扇区的特点:扇区数据仅有512字节......
  • 论文翻译: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的......
  • 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......
  • C++数据结构考研chapter5树(更新ing)
    一、概念1.结点2.边3.根4.叶子结点5.分支结点6.子树二、术语1.结点之间的关系描述(1)祖先(2)子孙(3)双亲(父)(4)孩子(5)兄弟(6)堂兄弟(7)路径自上而下(8)路径长度经过了几条边2.结点、树的属性描述(1)结点的层次(深度)从上到下数,默认从1开始,看题目要求(2)结点的高度从下到上......
  • chapter5-线性数据结构
    1.向量向量(vector)是可以改变其大小的线性序列容器。像数组一样,向量使用连续的空间存储元素,这表明向量也可以像数组一般通过其下标来访问其元素。但与数组不同的是,向量的大小可以动态变化。向量在内部使用动态数组的方式来存储元素,无需关心实现细节。(平均意义下,向量插入元素的时......