SML/NJ的简单功能 [英] Simple functions for SML/NJ

查看:97
本文介绍了SML/NJ的简单功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求为课堂上的问题写一套函数.我认为我写这些书的方式比他们需要的要复杂一些.我必须自己实现所有功能,而无需使用和预先定义的功能.我想知道这些答案是否有任何简单的单行"版本?

I was required to write a set of functions for problems in class. I think the way I wrote them was a bit more complicated than they needed to be. I had to implement all the functions myself, without using and pre-defined ones. I'd like to know if there are any quick any easy "one line" versions of these answers?

  1. 集可以表示为列表.集合的成员可以按任何顺序出现在列表中,但不应超过一个列表中元素的出现.

(a)定义dif(A,B)为计算A和B的集合差A-B.

(a) Define dif(A, B) to compute the set difference of A and B, A-B.

(b)定义笛卡尔(A,B)计算集合A和集合B的笛卡尔积,{(a,b)|a∈A,b∈B}.

(b) Define cartesian(A, B) to compute the Cartesian product of set A and set B, { (a, b) | a∈A, b∈B }.

(c)考虑以下内容:如果集合A具有n个元素,则A的幂集具有2n元素.根据证明,定义powerset(A)来计算集合A的幂集,{B |B⊆A}.

(c) Consider the mathematical-induction proof of the following: If a set A has n elements, then the powerset of A has 2n elements. Following the proof, define powerset(A) to compute the powerset of set A, { B | B ⊆ A }.

(d)定义一个给定的函数集合A和自然数k,返回的所有子集的集合大小为k的A.

(d) Define a function which, given a set A and a natural number k, returns the set of all the subsets of A of size k.

(* Takes in an element and a list and compares to see if element is in list*)

fun helperMem(x,[]) = false
|   helperMem(x,n::y) =
      if x=n then true
      else helperMem(x,y);

(* Takes in two lists and gives back a single list containing unique elements of each*)

fun helperUnion([],y) = y
|   helperUnion(a::x,y) =
       if helperMem(a,y) then helperUnion(x,y)
       else a::helperUnion(x,y);

(* Takes in an element and a list. Attaches new element to list or list of lists*)
 fun helperAttach(a,[]) = []
  helperAttach(a,b::y) = helperUnion([a],b)::helperAttach(a,y);


  (* Problem 1-a *)
fun myDifference([],y) = []
 |   myDifference(a::x,y) = 
    if helper(a,y) then myDifference(x,y)
    else a::myDifference(x,y);

 (* Problem 1-b *)
 fun myCartesian(xs, ys) =
    let fun first(x,[]) = []
    |       first(x, y::ys) = (x,y)::first(x,ys)
        fun second([], ys) = []
    |       second(x::xs, ys) = first(x, ys) @ second(xs,ys)
    in      second(xs,ys) 
    end;


 (* Problem 1-c *)
 fun power([]) = [[]]
 |   power(a::y) = union(power(y),insert(a,power(y)));

我从来没有遇到问题1-d,因为这些花费了我一段时间.关于减少这些较短的任何建议?我还没有遇到另一个问题,但是我想知道如何解决这个问题以便将来进行测试.

I never got to problem 1-d, as these took me a while to get. Any suggestions on cutting these shorter? There was another problem that I didn't get, but I'd like to know how to solve it for future tests.

  1. (楼梯问题)您想爬上n(> 0)步的楼梯.一次您可以执行一个步骤,两个步骤或三个步骤.但,例如,如果只剩下一步,则只能走一步步骤,而不是两三个步骤.有多少种不同的方法可以上楼梯?使用sml解决此问题.(a)解决递归地(b)迭代解决.

有关如何解决此问题的任何帮助?

Any help on how to solve this?

推荐答案

您的设置函数看起来不错.除了它们的格式和命名之外,我不会更改它们的任何主要原理:

Your set functions seem nice. I would not change anything principal about them except perhaps their formatting and naming:

fun member (x, [])    = false
  | member (x, y::ys) = x = y orelse member (x, ys)

fun dif ([],   B) = []
  | dif (a::A, B) = if member (a, B) then dif (A, B) else a::dif(A, B)

fun union ([],   B) = B
  | union (a::A, B) = if member (a, B) then union (A, B) else a::union(A, B)

(* Your cartesian looks nice as it is. Here is how you could do it using map: *)
local val concat = List.concat
      val map = List.map
in fun cartesian (A, B) = concat (map (fn a => map (fn b => (a,b)) B) A) end

您的力量也很整齐.如果调用函数 insert ,则该函数值得一提,有关将某些内容插入许多列表中.也许 insertEach 或类似名称是更好的名称.

Your power is also very neat. If you call your function insert, it deserves a comment about inserting something into many lists. Perhaps insertEach or similar is a better name.

在上一个任务中,由于这是一个计数问题,因此您无需生成实际的步骤组合(例如,作为步骤列表),只需对它们进行计数即可.使用递归方法,尝试按问题描述中的描述写下基本案例.

On your last task, since this is a counting problem, you don't need to generate the actual combinations of steps (e.g. as lists of steps), only count them. Using the recursive approach, try and write the base cases down as they are in the problem description.

即,使函数 steps:int->int ,其中预先计算了采取0、1和2步的方式,但是对于 n 步, n> 2 ,您知道是一组以1、2或3个步骤开头的步骤组合,以及采取 n-1 n-2 n-的数字组合3 步骤.

I.e., make a function steps : int -> int where the number of ways to take 0, 1 and 2 steps are pre-calculated, but for n steps, n > 2, you know that there is a set of combinations of steps that begin with either 1, 2 or 3 steps plus the number combinations of taking n-1, n-2 and n-3 steps respectively.

使用迭代方法,从底部开始,并使用参数化的计数变量.(很抱歉这里含糊不清.)

Using the iterative approach, start from the bottom and use parameterised counting variables. (Sorry for the vague hint here.)

这篇关于SML/NJ的简单功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆