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

Cornell cs3110 - Chapter4 Exercises

时间:2024-10-02 22:11:14浏览次数:8  
标签:map fold Cornell Chapter4 List fun let Exercises Exercise

(* Exercise: mystery operator 1 *)
let ( $ ) f x = f x;;
(* 使得函数的连续调用具有一部分右结合的特质 square $ 2 + 2 与 square 2 + 2 的运行结果分别是 16 和 6 *)

(* Exercise: repeat *)
let rec repeat f n x = 
  match n with
  | 0 -> x
  | _ -> repeat f (n - 1) (f x);;

(* Exercise: product *)
let product = function 
  | [] -> 1
  | h :: t -> List.fold_left ( * ) h t;;

(* Exercise: terse product *)
let terse_product = List.fold_left ( * ) 1;; (* Orz 我宣布 Ocaml 是世界上最美的语言 *)

(* Exercise: sum_cube_odd *)
let rec ( -- ) i j = if i > j then [] else i :: (i + 1 -- j);;

let odd x = if x mod 2 = 1 then true else false;;

let cube x = x * x * x;;

let sum = List.fold_left ( + ) 0;;

let sum_cube_add n = sum @@ List.map cube @@ List.filter odd @@ ( -- ) 0 n;;

(* Exercise: sum_cube_odd pipeline *)
let sum_cube_odd_pipe n = ( -- ) 0 n |> List.filter odd |> List.map cube |> List.fold_left ( + ) 0;;

(* Exercise: exists *)
let rec exists_rec f = function
  | [] -> false
  | h :: t -> if f h then true else (exists_rec f t);;

let exists_fold f = List.fold_left (fun x y -> x || f y) false;;

let exists_libs f l = List.fold_left ( || ) false @@ List.map f l;;

(* Exercise: library uncarried *)
let uncarried_append (l1, l2) = List.append l1 l2;;

(* Exercise: map composition *)
let map_composition = fun f g l -> List.map (fun x -> f (g x)) l;; (* f @@ g 好像无法通过类型推断 solution 应该是错了 具体解释等我学完解释器 :) *)

let map_composition_test = map_composition (fun x -> x + 1) (fun x -> x * x) [1; 2; 3; 4; 5];;

(* Exercise: more list fun *)
let greater_then_3 = List.filter (fun x -> x > 3);;

let add_1 = List.map (fun x -> x +. 1.0);;

let gather_sentence strs sep = List.fold_left (fun x y -> if x = "" then y else x ^ sep ^ y) "" strs;;

(* Exercise: association list keys *)
let keys : ('a * 'b) list -> 'a list = fun lst -> List.map (fun (x, y) -> x) lst |> List.sort_uniq compare;;

(* Exercise: valid matrix *)
let is_valid_matrix = function 
  | [] -> false 
  | m -> List.map List.length m |> List.sort_uniq compare |> List.length = 1;;

(* Exercise: row vector add *)
let add_row_vectors v1 v2 = List.map2 ( + ) v1 v2;;

(* Exercise: matrix add *)
let add_matrices m1 m2 = List.map2 add_row_vectors m1 m2;;

(* Exercise: matrix multiply *)
let transpose m = List.map (fun i -> List.map (fun x -> List.nth x i) m) (0 -- (List.length (List.hd m) - 1));;

let mul_row_vectors v1 v2 = List.fold_left ( + ) 0 @@ List.map2 ( * ) v1 v2;;

let multiply_matrices m1 m2 = List.map (fun x -> List.map (fun y -> mul_row_vectors x y) (transpose m2)) m1;;

标签:map,fold,Cornell,Chapter4,List,fun,let,Exercises,Exercise
From: https://www.cnblogs.com/sysss-blogs/p/18445178

相关文章

  • 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,......
  • Chapter4 Writing Your First Kernel Module - LKMs Part 1
    Chapter1KernelWorkspaceSetupChapter2Buildingthe5.xLinuxKernelfromSource,Part1Chapter3Buildingthe5.xLinuxKernelfromSource,Part2Chapter4WritingYourFirstKernelModule–LKMsPart1Chapter5WritingYourFirstKernelModule–......
  • 论文翻译: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......
  • chapter4-字符串
    记录字符串常考的4种操作:遍历、加密、统计、匹配。1.字符串介绍C++提供了字符串(string)这种基本数据类型,它可以很方便地对字符串进行各种操作。使用需要添加头文件#include<string>。1.1字符串的构造字符串的构造包括定义和初始化两个部分,定义一个字符串的方式和定义其他基本......
  • cs3110-2.6exercises
    Exercise:values[✭]WhatisthetypeandvalueofeachofthefollowingOCamlexpressions?7*(1+2+3)"CS"^string_of_int3110Hint:typeeachexpressionintothetoplevelanditwilltellyoutheanswer.Note:^isnotexponentiation.int......
  • 读后笔记 -- Pytest框架与自动化应用 Chapter4:DDT 和 参数传递
    4.2参数化应用1.单一参数化/多参数化#contentoftest_mark_parametrize.pymportpytest@pytest.mark.parametrize("test_case",[1,2,3,'orange','apple'])deftest_string(test_case):#单一参数化print(f"\n我们的测试数据:{test_case}&q......