总和SML中的两个列表

发布于 2025-02-02 12:13:32 字数 245 浏览 3 评论 0原文

SML中有两个列表,我想在另一个列表中添加一个列表,

val one = [1.1,2.2,3.3] : real list;
val two = [4.4,5.5,6.6] : real list;

结果应该是[5.5,7.7,9.9]。

我不确定我正在做正确的事情,但基本上我正在尝试将这两个列表传递给一个乐趣,然后做一个[0] +两个[0]之类的事情,

还有更好的方法吗?谢谢〜

There are two list in sml and I want to add one to another

val one = [1.1,2.2,3.3] : real list;
val two = [4.4,5.5,6.6] : real list;

The result should be [5.5, 7.7, 9.9].

I not sure I'm doing this right but basically I'm trying to pass this two list to a fun then do things like one[0] + two[0]

Is there any better way to do it? thanks~

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一笑百媚生 2025-02-09 12:13:32

最简洁的解决方案是使用listPair.map函数:

ListPair.map Real.+ (one, two)

如果一个列表比另一个列表更长,则此解决方案会丢弃多余的元素。

The most concise solution is to use the ListPair.map function:

ListPair.map Real.+ (one, two)

This solution discards the excess elements if one list is longer than the other.

夏花。依旧 2025-02-09 12:13:32

SML列表通常不会通过索引访问。列表是 empty 或一个值和列表。使用此递归定义,我们可以有任何长度的列表。这也意味着我们只需要在两种情况下操作以迭代列表:空列表或带有元素和另一个列表的列表。

考虑MAP函数,该功能将函数f映射到列表,然后返回另一个列表。

fun map f []      = []
  | map f (x::xs) = f x :: map f xs

我们可以在int list中的每个元素中添加1个,其中类似于以下内容,

map (fn x => x + 1) [2, 3, 4]

它将产生[3,4,5]

映射在两个列表上非常简单。

fun map2 f [] []           = [] 
  | map2 f (x::xs) (y::ys) = (* ... *)

尽管这不能容纳不同长度的列表或如何处理它们。

如果您能弄清楚这一点,那么完成分配的任务是map2的非常简单的应用。

SML lists are not typically accessed by index. A list is either empty or a value and a list. With this recursive definition we can have a list of any length. It also means we only need to operate on two cases to iterate over a list: the empty list, or a list with an element and another list.

Consider a map function that maps a function f to a list and returns another list.

fun map f []      = []
  | map f (x::xs) = f x :: map f xs

We could add 1 to every element in an int list with something like:

map (fn x => x + 1) [2, 3, 4]

Which would yield [3, 4, 5].

It's pretty straightforward to map over two lists.

fun map2 f [] []           = [] 
  | map2 f (x::xs) (y::ys) = (* ... *)

Though this does not accommodate lists of differing lengths or how to handle them.

If you can figure this out, accomplishing your assigned task is a very straightforward application of map2.

自此以后,行同陌路 2025-02-09 12:13:32

如何遵循...

fun add_lists any [] = any
  | add_lists [] any = any
  | add_lists (a::bs) (c::ds): real list = (a + c) :: add_lists bs ds;

测试

> val add_lists = fn: real list -> real list -> real list
> add_lists [] [];
> val it = []: real list;
> add_lists [1.0] [];
> val it = [1.0]: real list;
> add_lists [] [2.1];
> val it = [2.1]: real list;
> add_lists [1.3] [2.0];
> val it = [3.3]: real list;
> add_lists [1.1, 2.2] [3.2, 4.5];
> val it = [4.300000000000001, 6.7]: real list;
> add_lists [1.3] [2.7, 3.5];
> val it = [4.0, 3.5]: real list;
> add_lists [] [2.3, 3.2];
> val it = [2.3, 3.2]: real list;
> add_lists [1.2, 2.1] [];
> val it = [1.2, 2.1]: real list;
> add_lists [1.0, 2.0, 3.0] [4.0, 5.0, 6.0];
> val it = [5.0, 7.0, 9.0]: real list;
> add_lists [1.1,2.2,3.3] [4.4,5.5,6.6];
> val it = [5.5, 7.7, 9.899999999999999]: real list;

插图

let initialState = Interpreter.getFirstState();
let interpretationResult = Interpreter.interpret('', initialState);

function eval_code(code) {
  let input_provider = document.querySelector('#interpreter-input-provider');
  let code_to_execute = (code || input_provider.value || '').trim() + ';';

  if (code_to_execute) {
    try {
      interpretationResult = Interpreter.interpret(code_to_execute, interpretationResult.state);
      console.log(interpretationResult.state.toString());

      if (interpretationResult.warnings && interpretationResult.warnings.length) {
        interpretationResult.warnings.forEach(warning => console.log('[Evaluation Warning]\n', warning.toString()));
      }
    } catch (error) {
      console.error('[Evaluation Error]\n', error.message);
    }

    input_provider.value = '';
  }
}

function eval_definition() {
  const fun_definition = `fun add_lists any [] = any
  | add_lists [] any = any
  | add_lists (a::bs) (c::ds): real list = (a + c) :: add_lists bs ds;`;
  eval_code(fun_definition);
}

function run_tests() {
  const input_array = [
    "add_lists [] [];",
    "add_lists [1.0] [];",
    "add_lists [] [2.1];",
    "add_lists [1.3] [2.0];",
    "add_lists [1.1, 2.2] [3.2, 4.5];",
    "add_lists [1.3] [2.7, 3.5];",
    "add_lists [] [2.3, 3.2];",
    "add_lists [1.2, 2.1] [];",
    "add_lists [1.0, 2.0, 3.0] [4.0, 5.0, 6.0];",
    "add_lists [1.1,2.2,3.3] [4.4,5.5,6.6];"
  ];

  input_array.forEach(input => eval_code(input));
}
eval_definition();
run_tests();

// interpretationResult = Interpreter.interpret('fun f y = x | f 10 = "???";', interpretationResult.state);
// console.log(interpretationResult.warnings);
#interpreter-input-provider {
  display: block;
  // min-width: 200px;
  //min-height: 30%;
  width: 90%
}

button {
  width: 20%;
  height: 3em;
  cursor: pointer;
}
<script src="https://unpkg.com/@sosml/interpreter@^1.5.0/build/interpreter.min.js"></script>

<section id="interpretation-input">
  <button id="eval-top" onclick="eval_code();">Eval</button>
  <textarea id="interpreter-input-provider" rows="20">

  </textarea>
  <button id="eval-bottom" onclick="eval_code();">Eval</button>

</section>
<section id="interpretation-output">

</section>

<code id="fun-definition" hidden>
fun add_lists any [] = any
  | add_lists [] any = any
  | add_lists (a::bs) (c::ds): real list = (a + c) :: add_lists bs ds;
</code>

How about following...

fun add_lists any [] = any
  | add_lists [] any = any
  | add_lists (a::bs) (c::ds): real list = (a + c) :: add_lists bs ds;

Test

> val add_lists = fn: real list -> real list -> real list
> add_lists [] [];
> val it = []: real list;
> add_lists [1.0] [];
> val it = [1.0]: real list;
> add_lists [] [2.1];
> val it = [2.1]: real list;
> add_lists [1.3] [2.0];
> val it = [3.3]: real list;
> add_lists [1.1, 2.2] [3.2, 4.5];
> val it = [4.300000000000001, 6.7]: real list;
> add_lists [1.3] [2.7, 3.5];
> val it = [4.0, 3.5]: real list;
> add_lists [] [2.3, 3.2];
> val it = [2.3, 3.2]: real list;
> add_lists [1.2, 2.1] [];
> val it = [1.2, 2.1]: real list;
> add_lists [1.0, 2.0, 3.0] [4.0, 5.0, 6.0];
> val it = [5.0, 7.0, 9.0]: real list;
> add_lists [1.1,2.2,3.3] [4.4,5.5,6.6];
> val it = [5.5, 7.7, 9.899999999999999]: real list;

Illustration

let initialState = Interpreter.getFirstState();
let interpretationResult = Interpreter.interpret('', initialState);

function eval_code(code) {
  let input_provider = document.querySelector('#interpreter-input-provider');
  let code_to_execute = (code || input_provider.value || '').trim() + ';';

  if (code_to_execute) {
    try {
      interpretationResult = Interpreter.interpret(code_to_execute, interpretationResult.state);
      console.log(interpretationResult.state.toString());

      if (interpretationResult.warnings && interpretationResult.warnings.length) {
        interpretationResult.warnings.forEach(warning => console.log('[Evaluation Warning]\n', warning.toString()));
      }
    } catch (error) {
      console.error('[Evaluation Error]\n', error.message);
    }

    input_provider.value = '';
  }
}

function eval_definition() {
  const fun_definition = `fun add_lists any [] = any
  | add_lists [] any = any
  | add_lists (a::bs) (c::ds): real list = (a + c) :: add_lists bs ds;`;
  eval_code(fun_definition);
}

function run_tests() {
  const input_array = [
    "add_lists [] [];",
    "add_lists [1.0] [];",
    "add_lists [] [2.1];",
    "add_lists [1.3] [2.0];",
    "add_lists [1.1, 2.2] [3.2, 4.5];",
    "add_lists [1.3] [2.7, 3.5];",
    "add_lists [] [2.3, 3.2];",
    "add_lists [1.2, 2.1] [];",
    "add_lists [1.0, 2.0, 3.0] [4.0, 5.0, 6.0];",
    "add_lists [1.1,2.2,3.3] [4.4,5.5,6.6];"
  ];

  input_array.forEach(input => eval_code(input));
}
eval_definition();
run_tests();

// interpretationResult = Interpreter.interpret('fun f y = x | f 10 = "???";', interpretationResult.state);
// console.log(interpretationResult.warnings);
#interpreter-input-provider {
  display: block;
  // min-width: 200px;
  //min-height: 30%;
  width: 90%
}

button {
  width: 20%;
  height: 3em;
  cursor: pointer;
}
<script src="https://unpkg.com/@sosml/interpreter@^1.5.0/build/interpreter.min.js"></script>

<section id="interpretation-input">
  <button id="eval-top" onclick="eval_code();">Eval</button>
  <textarea id="interpreter-input-provider" rows="20">

  </textarea>
  <button id="eval-bottom" onclick="eval_code();">Eval</button>

</section>
<section id="interpretation-output">

</section>

<code id="fun-definition" hidden>
fun add_lists any [] = any
  | add_lists [] any = any
  | add_lists (a::bs) (c::ds): real list = (a + c) :: add_lists bs ds;
</code>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文