Sml 为什么在标准ML的这段代码中不断出现错误?

Sml 为什么在标准ML的这段代码中不断出现错误?,sml,ml,Sml,Ml,我正在学习标准ML,我不断地得到这个错误,我不知道为什么 以下是代码和错误: > fun in_list(element, list) = if hd(list) = element then true else val tempList = List.drop(list, 1); in_list(element, tempList); # # Error-Expression expected but val was found Static Errors 我

我正在学习标准ML,我不断地得到这个错误,我不知道为什么

以下是代码和错误:

> fun in_list(element, list) = 
    if hd(list) = element then true
    else val tempList = List.drop(list, 1);
    in_list(element, tempList);
# # Error-Expression expected but val was found
Static Errors

我知道我尝试的语法肯定有问题。

您需要将
val
值包装在
let..in..end
块中

fun in_list(element, list) = 
    if hd(list) = element then true
    else 
        let
           val tempList = List.drop(list, 1)
        in
           in_list(element, tempList)
        end
此外,
hd
drop
不建议分解列表。您应该改用模式匹配

fun in_list(element, x::xs) = 
    if x = element then true
    else in_list(element, xs)

有一个缺少空列表的基本情况,您可以使用
orelse
替换
如果x=element则为true…
。我将它们作为建议留给您。

您需要将
val
值包装在
let..in..end
块中

fun in_list(element, list) = 
    if hd(list) = element then true
    else 
        let
           val tempList = List.drop(list, 1)
        in
           in_list(element, tempList)
        end
此外,
hd
drop
不建议分解列表。您应该改用模式匹配

fun in_list(element, x::xs) = 
    if x = element then true
    else in_list(element, xs)

有一个缺少空列表的基本情况,您可以使用
orelse
替换
如果x=element则为true…
。我把它们留给您作为建议。

哦,太酷了,x::xs是做什么的?我知道“:”意味着犯罪。另外,如何让它返回false?
是cons构造函数,用于将非空列表分解为头
x
和尾
xs
。列表是使用空列表
[]
::
构建的。例如,
[1,2,3]
实际上是
1::2::3::[]
。好吧,这很有意义,如果它不是真的,我如何让函数返回false,因为我们使用了“then”关键字来返回真的情况。正如我在回答中所说的,你错过了一个基本情况
有趣的列表(元素,[])=false(元素,x::xs中)=…
。如果输入列表为空,只需返回
false
。哦,酷,x::xs做什么?我知道“:”意味着犯罪。另外,如何让它返回false?
是cons构造函数,用于将非空列表分解为头
x
和尾
xs
。列表是使用空列表
[]
::
构建的。例如,
[1,2,3]
实际上是
1::2::3::[]
。好吧,这很有意义,如果它不是真的,我如何让函数返回false,因为我们使用了“then”关键字来返回真的情况。正如我在回答中所说的,你错过了一个基本情况
有趣的列表(元素,[])=false(元素,x::xs中)=…
。如果输入列表为空,只需返回
false