讲解:CSE 216、Python、polymorphic、PythonHaskell|Prolog

CSE 216 – Homework IThis homework document consists of 3 pages. Carefully read the entire document before you start coding.Note: All functions, unless otherwise specified, should be polymorphic (i.e., they should work with anydata type). For example, if you are writing a method that should work for lists, the type must be a list,and not int list.1 Recursion and Higher-order Functions (56 points)In this section, you may not use any functions available in the OCaml library that already solves all or mostof the question. For example, OCaml provides a List.rev function, but you may not use that in this section.1. Write a recursive function pow, which takes two integer parameters x and n, and returns x (5) n. Also write afunction float pow, which does the same thing, but for x being a float. n is still a non-negative integer.2. Write a function compress to remove consecutive duplicates from a list. (5)# compress [a;a;b;c;c;a;a;d;e;e;e];;- : string list = [a; b; c; a; d; e]3. Write a function remove if of the type a list -> (a -> bool) -> a list, which takes a list and (5)a predicate, and removes all the elements that satisfy the condition expressed in the predicate.# remove_if [1;2;3;4;5] (fun x -> x mod 2 = 1);;- : int list = [2; 4]4. Some programming languages (like Python) allow us to quickly slice a list based on two integers i and (5)j, to return the sublist from index i (inclusive) and j (not inclusive). We want such a slicing functionin OCaml as well.Write a function slice as follows: given a list and two indices, i and j, extract the slice of the listcontaining the elements from the ith (inclusive) to the jth (not inclusive) positions in the original list.# slice [a;b;c;d;e;f;g;h] 2 6;;- : string list = [c; d; e; f]Invalid index arguments should be handled gracefully. For example,# slice [a;b;c;d;e;f;g;h] 3 2;;- : string list = []# slice [a;b;c;d;e;f;g;h] 3 20;- : string list = [d;e;f;g;h];You do not, however, need to worry about handling negative indices.5. Write a function equivs of the type (a -> a -> bool) -> a list -> a list list, which par- (6)titions a list into equivalence classes according to the equivalence function.# equivs (=) [1;2;3;4];;- : int list list = [[1];[2];[3];[4]]# equivs (fun x y -> (=) (x mod 2) (y mod 2)) [1; 2; 3; 4; 5; 6; 7; 8];;- : int list list = [[1; 3; 5; 7]; [2; 4; 6; 8]]6. Goldbach’s conjecture states that every positive even number greater than 2 is the sum of two prime (6)numbers. E.g., 18 = 5 + 13, or 42 = 19 + 23. It is one of the most famous conjectures in number theory.It is unproven, but verified for all integers up to 4 × 1018. Write a function goldbachpair : int ->int * int to find two prime numbers that sum up to a given even integer. The returned pair must havea non-decreasing order.1CSE 216 Homework I Submission Deadline: Feb 29, 2020# goldbachpair 10;; (* must return (3, 7) and not (7, 3) *)- : int * int = (3, 7)Note that the decomposition is not always unique. E.g., 10 can be written as 3+7 or as 5+5, so both(3, 7) and (5, 5) are correct answers.7. Write a function called equiv on, which takes three inputs: two functions f and g, and a list lst. It (6)returns true if and only if the functions f and g have identical behavior on every element of lst.# let f i = i * i;;val f : int -> int = # let g i = 3 * i;;val g : int -> int = # equiv_on f g [3];;- : bool = true# equiv_on f g [1;2;3];;- : bool = false8. Write a functions called pairwisefilter with two parameters: (i) a function cmp that compares two (6)elements of a specific T and returns one of them, and (ii) a list lst of elements of that same type T. Itreturns a list that applies cmp while taking two items at a time from lst. If lst has odd size, the lastelement is returned “as is”.# pairwisefilter min [14; 11; 20; 25; 10; 11];;- : int list = [11; 20; 10]# (* assuming that shorter : string * string -> string = already exists *)# pairwisefilter shorter [and; this; makes; shorter; strings; always; win];;- : string list = [and; makes; always; win]9. Write the polynomial function, which takes a list of tuples and returns the polynomial function corre- (6)sponding to that list. Each tuple in the input list consists of (i) the coefficient, and (ii) the exponent.# (* below is the polynomial function f(x) = 3x^3 - 2x + 5 *)# let f = polynomial [3, 3; -2, 1; 5, 0];;val f : int -> int = # f 2;;- : int = 2510. The power set of a set S is the set of all subsets of S (incluCSE 216作业代做、Python程序设计作业调试、代写polymorphic作业、代写Python语言作业 帮做Hading the empty set and the entire set). (6)Write a function powerset of the type a list -> a list list, which treats lists as unordered sets,and returns the powerset of its input list. You may assume that the input list has no duplicates.# powerset [3; 4; 10];;- : int list list = [[]; [3]; [4]; [10]; [3; 4]; [3; 10]; [4; 10]; [3; 4; 10]];2 Data Types (40 points)1. Let us define a language for expressions in Boolean logic: (10)type bool_expr =| Lit of string| Not of bool_expr| And of bool_expr * bool_expr| Or of bool_expr * bool_exprusing which we can write expressions in prefix notation. E.g., (a∧b)∨(¬a) is Or(And(Lit(a), Lit(b)),Not(Lit(a))). Your task is to write a function truth table, which takes as input a logical expressionin two literals and returns its truth table as a list of triples, each a tuple of the form:(truth-value-of-first-literal, truth-value-of-second-literal, truth-value-of-expression)For example,# (* the outermost parentheses are needed for OCaml to parse the third argumentcorrectly as a bool_expr *)# truth_table a b (And(Lit(a), Lit(b)));;- : (bool * bool * bool) list = [(true, true, true); (true, false, false);(false, true, false); (false, false, false)]2CSE 216 Homework I Submission Deadline: Feb 29, 20202. In this question you will use higher-order functions to implement an interpreter for a simple stack-based (30)evaluation language. This language has a fixed set of commands:• start → Initializes an empty stack. This is always the first command in a program and neverappears again.• (push n) → Pushes the specified integer n on to the top of the stack. This command is alwaysparenthesized.• pop → Removes the top element of the stack.• add → Pops the top two elements of the stack, computes their sum, and pushes the result back onto the stack.• mult → Pops the top two elements of the stack, computes their product, and pushes the result backon to the stack.• clone → Pushes a duplicate copy of the top element on to the stack.• kpop → Pops the top element k of the stack, and if k is a positive number, pops k more elements(or the stack becomes empty, whichever happens sooner).• halt → Terminates the stack evaluation program. This is always the last command.Your task is to define the stack data structure in OCaml (10 points), and then implement the abovecommands (20 points). Your stack must be implemented as a list. A complete running example wouldlook something like the following:# start (push 2) (push 3) (push 4) mult add ha<;- : list int = [14]You may assume that only valid commands will be provided. As such, you do not have to worry aboutexception handling.3 Version Control (4 points)For this part, you must use a Bitbucket account with your stonybrook.edu email address. You should alreadyhave one, based on the first recitation task. You must create a repository with the name CSE216HW1on Bitbucket and give access to your grading TA (your grading TA will be allocated based on your last name,and will be announced on Blackboard by the end of this week). Access must be given to your grading TAbefore the homework deadline. Otherwise, this component of the homework will not be graded. We stronglyencourage you to commit and push your code to this remote repository every time you finish solving anyquestion, an even more frequently for the longer questions (e.g., 2.2). Your grading TA will check for thefollowing:- There must be at least 4 distinct code pushes to the remote repository, with each push safely committingthe solution to at least one new solution to a homework question. [2 points]- At least 2 code pushes to the remote repository must be made on separate days. [1 point]- At least 2 code pushes must include meaningul commit messages describing what code has been contributedsince the previous version. [1 point]NOTES:• Late submissions or uncompilable code will not be graded.• Please remember to verify what you are submitting. Make sure you are, indeed, submitting what youthink you are submitting!• What to submit? A single .zip file comprising three .ml files. The first file must be named hw1.ml,and should contain your code for the ten questions in section 1 of this assignment. The second file mustbe named hw1bool.ml, and this should contain your code for question 2.1 (Boolean logic). Finally, thethird file must be named hw1stack.ml, with your code for question 2.2 (stack evaluation language).This assignment will be graded by a script, so be absolutely sure that the submissionfollows this structure (including the name of your remote repository).Submission Deadline: Feb 29, 2020, 11:59 pm3转自:http://www.6daixie.com/contents/3/4959.html

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,258评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,335评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,225评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,126评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,140评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,098评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,018评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,857评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,298评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,518评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,678评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,400评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,993评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,638评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,801评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,661评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,558评论 2 352

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,451评论 0 13
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,695评论 0 3
  • day1 五岁,奶奶姑姑送我上幼儿园,听话又调皮,没有哭闹,被老师安排到一个一人桌上,给了我两块橡皮泥(这里怎么玩...
    墨白rapper阅读 258评论 0 2
  • 难得这样 黄绮晗 夏天的夜晚,在我的印象里,总不会是干净清爽的模样。人们三五成群、勾肩搭背,流连于这座城市的各个角...
    简约语文阅读 1,941评论 0 2
  • 今天董事长跟一个财务储备说,要把一些事情简单化,让每个人都能做,都能懂~然后再转化为复杂化、标准化,去应对每一...
    雨文_yuwencc1009阅读 383评论 0 1