简体   繁体   English

在OCaml中从值列表中取值

[英]Taking values out of list in OCaml

I am creating a list of lists in OCaml and I am having trouble understanding how I would accomplish my goal. 我在OCaml中创建了一个列表列表,我无法理解如何实现目标。

My goal is to preform the following: 我的目标是预先形成以下内容:

Given V = (0 1 1 -1) as a list: 给定V =(0 1 1 -1)作为列表:

Preform this equation: 预先形成这个等式:

(2*V1 - 1)*(2*V2-1) = ?
(2*V1 - 1)*(2*V3-1) = ?
(2*V1 - 1)*(2*V4-1) = ?
(2*V2 - 1)*(2*V3-1) = ?
(2*V1 - 1)*(2*V4-1) = ?
(2*V3 - 1)*(2*V4-1) = ?

Then translate this into a list of list such as 然后将其转换为列表,例如

0  V12 V13 V14
V21 0  V23 V24
V31 V32 0  V34
V41 V42 V43 0

Where the matrix is a reflection of itself. 矩阵是自身的反映。

I am going to use 2 functions do accomplish this. 我将使用2个函数来完成此任务。

First one gives the output for one state and the other gives the whole matrix. 第一个给出一个状态的输出,另一个给出整个矩阵。

Here is what I have going so far: 这是我到目前为止所做的事情:

singleState state =
if state = 0.0 then 0.0
else
    ((2*state) -. 1);;

matrixState V =
if V = [] then []
else
    singleState(hd(V)) * singleState(next item after head) + matrixState(tl(V));;

As you can see I cant figure out how to take the item after the head of V. Such as V2. 正如你所看到的,我无法弄清楚如何在V的头部之后拿走这个项目。比如V2。

Any suggestions for me? 对我有什么建议吗?

This is what I have so far: 这是我到目前为止:

let singleState state = 
    if state = 0.0 then 0.0
    else
        ((2.0 *. state) -. 1.0);;


let rec matrixState V = 
    match V with
    | [] -> []
    | hd :: [] -> V
    | hd :: (nx :: _ as tl) ->
        singleState hd *. singleState nx :: matrixState tl;;

Your best bet is to learn to think in terms of pattern matching. 你最好的选择是学习模式匹配方面的思考。 Instead of using = to test whether V is empty, you can write something more like this: 而不是使用=来测试V是否为空,您可以编写更像这样的东西:

let rec matrixState v =
    match v with
    | [] -> []
    | hd :: [] -> (* Not sure what you want here *)
    | hd :: (nx :: _ as tl) ->
        singleState hd *. singleState nx :: matrixState tl

First of all, keep in mind that this is just a sketch of a function that does what you seem to want your matrixState function to do. 首先,请记住,这只是一个函数的草图,它可以执行您希望matrixState函数执行的操作。 I'm not sure how useful it will be in the final answer to your problem. 我不确定在你的问题的最终答案中它会有多大用处。

What this function does is match v (a list) against a series of patterns, and give a different answer depending on which pattern matches. 这个函数的作用是将v(列表)与一系列模式相匹配,并根据哪个模式匹配给出不同的答案。

The first pattern ( [] ) matches an empty list. 第一个模式( [] )匹配一个空列表。 As in your code, the result is an empty list. 与在代码中一样,结果是一个空列表。

The second pattern ( hd :: [] ) matches a list with just one element in it. 第二个模式( hd :: [] )匹配一个只包含一个元素的列表。 I'm not sure what you want to do for this case, so I didn't write the code. 我不确定你想为这种情况做什么,所以我没有写代码。

The third pattern ( hd :: (nx :: _ as tl) ) matches a list with two or more elements. 第三种模式( hd :: (nx :: _ as tl) )匹配具有两个或更多元素的列表。 It gives the name hd to the first element and the name nx to the second element. 它将名称hd赋予第一个元素,将名称nx赋予第二个元素。 Furthermore it gives the name tl to the tail of the list (everything except the first element). 此外,它将名称tl赋予列表的尾部(除第一个元素之外的所有内容)。 This is where you can see the power of OCaml patterns. 在这里您可以看到OCaml模式的强大功能。

Update 更新

(Note: you're supposed to use a SO page to ask one question that has a specific technical answer. You shouldn't use the same page to ask a series of questions. It makes it harder for people later to find answers. If you have a new question you should start it on a new page.) (注意:您应该使用SO页面来提出一个具有特定技术答案的问题。您不应该使用同一页面来提出一系列问题。这会使人们以后更难找到答案。如果你有一个新问题,你应该在新页面上启动它。)

In OCaml you use names starting with upper case for constructors, and names starting with lower case for ordinary variables. 在OCaml中,对于构造函数使用以大写字母开头的名称,对于普通变量使用以小写字母开头的名称。 Your code as you give it here won't compile because you're using upper case V as a variable. 您在此处提供的代码将无法编译,因为您使用大写字母V作为变量。 Note in my code above I used lower case v. 请注意我在上面的代码中使用了小写v。

If I change your upper case V to lower case, your code compiles fine for me. 如果我将大写字母V更改为小写字母,那么您的代码编译就可以了。 I don't get any errors. 我没有得到任何错误。

(Again, I'd recommend that you do some basic reading about OCaml. It's not to avoid helping you, but rather that I think it would help you more, and more quickly.) (再一次,我建议你做一些关于OCaml的基本阅读。这不是为了避免帮助你,而是我认为它可以帮助你更多,更快。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 OCaml 中传递变量的值 - How to pass values of variables in OCaml OCaml - 递归列表列表'如果表达式' - OCaml - list of lists recursive 'if expression' 替代if语句,用于采用多种形式的输入值 - Alternate to if statements for taking multiple form input values MATLAB:取列表中两个数字的平均值? - MATLAB: taking the average of two numbers in a list? 无法使用数组或foreach循环遍历数据列表并仅打印出某些值 - Cannot use arrays or foreach loops to iterate over list of data and print out only certain values 我们可以刷新数组中的值,是否从shell中的for循环中获取值 - Can we flush the values in an array, taking the values from a for loop in shell 从另一列中获取具有至少一个指定值的值 - Taking values with at least one specified values from another column 需要按等级过滤回复,去掉“false”和“n/a”的回复 - Need to filter responses by grade, taking out the "false" and n/a" responses 我如何取两列,一列用于名称,另一列用于值,并吐出每个唯一名称的唯一数量? 然后列个清单 - How do I take 2 columns, one for name and one for values, and spit out the unique number of amounts per unique name? Then make a list 列表索引超出范围? 如果陈述 - List index out of range? If statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM