0

我正在学习标准 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

我知道我正在尝试的语法一定有问题。

4

1 回答 1

3

您需要将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

此外,hddrop建议分解列表。您应该改用模式匹配。

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

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

于 2012-10-23T07:39:05.087 回答