简体   繁体   English

OCaml列表语法错误

[英]OCaml list syntax error

I am writing a function to remove neighbors from a list which have the same value. 我正在编写一个函数,以从具有相同值的列表中删除邻居。 I don't understand what the syntax error is here. 我不明白这里的语法错误。 This is what I have: 这就是我所拥有的:

let rec rem_dup_neighb l = 
let rec rem_dup_neighb_aux l lastseen retl = 
match l with 
 []->retl
|[()]->[()]
| (y::rest) -> if(y==lastseen) then rem_dup_neighb_aux l lastseen retl else rem_dup_neighb_aux l y (y::retl)
in rem_dup_neighb_aux l 9000 [];;

I get the following error for the last line of the function: 对于函数的最后一行,出现以下错误:

Error: This expression has type int but an expression was expected of type
     unit

As an example, if you pass in [1;2;2;3;5] to the function, it should return [1;2;3;5] 例如,如果将[1; 2; 2; 3; 5]传递给函数,则它应返回[1; 2; 3; 5]

Any help is appreciated. 任何帮助表示赞赏。 Thanks 谢谢

UPDATE: Function seems to be infinite looping: 更新:函数似乎是无限循环:

let rec rem_dup_neighb l = 
let rec rem_dup_neighb_aux l lastseen retl = 
match l with []->retl
| (y::rest) -> if(y==lastseen) then rem_dup_neighb_aux l lastseen retl else rem_dup_neighb_aux l y (y::retl)
in
    match l with
    (lastseen::rest) ->  rem_dup_neighb_aux l lastseen []

UPDATE 2: wasn't reducing the problem per iteration. 更新2:并没有减少每次迭代的问题。 Function seems to be returning [5;3;2] instead of [1;2;3;5] now though. 函数现在似乎返回[5; 3; 2],而不是[1; 2; 3; 5]。

let rec rem_dup_neighb l = 
let rec rem_dup_neighb_aux l lastseen retl = 
match l with []->retl
| (y::rest) -> if(y==lastseen) then rem_dup_neighb_aux rest lastseen retl else rem_dup_neighb_aux rest y (y::retl)
in
    match l with
    []->[]
    |(lastseen::rest) ->  rem_dup_neighb_aux l lastseen []

UPDATE 3: 更新3:

let rec rem_dup_neighb l = 
let rec rem_dup_neighb_aux l lastseen retl = 
match l with []->retl
| (y::rest) -> if(y==lastseen) then rem_dup_neighb_aux rest lastseen retl else rem_dup_neighb_aux rest y (y::retl)
in
    match l with
    []->[]
    |(lastseen::rest) ->  rem_dup_neighb_aux l lastseen [lastseen]

If one of the test cases has [()] as input, then you can't use 9000 as a fake value for the beginning of the list. 如果其中一个测试用例具有[()]作为输入,则不能将9000用作列表开头的假值。 You need to have a fake value of the same type as whatever is in the list. 您需要具有与列表中相同类型的假值。 One idea would be to use the actual first value of the list. 一个想法是使用列表的实际第一个值。

As a side comment, it seems like you should just take out your second case in the match. 顺带一提,似乎您应该把比赛中的第二个案件拿出来。 By the second case, I mean the pattern that (after fixing up the code) matches a list of length one; 对于第二种情况,我的意思是(固定代码之后)匹配长度为一的列表的模式; ie, the pattern [_] . 即模式[_] With your approach (using a "last seen" value) there are only two cases, for empty and non-empty lists. 使用您的方法(使用“最后看到的”值),只有两种情况:空列表和非空列表。 Your special problems come at the beginning of the list (as you're experiencing now), not at the end. 您的特殊问题排在列表的开头(就像您现在所经历的那样),而不是排在最后。

Update 更新

You can have a pattern match (a match expression) anywhere you like. 您可以在任何喜欢的地方进行模式匹配( match表达式)。 So, you can have one after the in : 所以,你可以有一个后in

let rem_dup_neighbors aList =
    let rec aux l lastseen retl =
        ...
    in
    match aList with
    | pattern1 -> blah blah blah
    | pattern2 -> aux blah blah

Update 2 更新2

When calling yourself recursivelly, you have to pass a smaller problem. 递归调用自己时,您必须传递一个较小的问题。 Your auxiliary function just keeps calling itself with the same list. 您的辅助功能只是不断使用相同的列表进行调用。

The match clause [()] -> [()] accepts and produces a unit list , which doesn't really make sense there. match子句[()] -> [()]接受并产生一个unit list ,在这里并没有什么意义。 You probably should have written [_] -> retl , _ being the match syntax for "match anything". 您可能应该已经编写了[_] -> retl_是用于“匹配任何内容”的匹配语法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM