简体   繁体   English

Haskell-统一将产生无限类型

[英]Haskell - unification would give infinite type

I am new to Haskell and I just encountered this problem/error. 我是Haskell的新手,我刚遇到此问题/错误。

I have not a single clue what's going on, also I am not really familiar using lists and defining them as (l:k) ... i don't really know what l and k are considered... l is an element and k is a list? 我不知道发生了什么,也不太熟悉使用列表并将它们定义为(l:k) ...我真的不知道l和k被认为是什么... l是一个元素,并且k是一个列表?

Anyways, I would appreciate someone explained to me these l and k things or maybe (l:t:k) inside a function using list and maybe a way to write this simple delete function which, given that the element is inside the list, finds the first appearance of the desired element and deletes it, returning the new list. 无论如何,我将不胜感激有人向我解释了这些l和k东西,或者可能是在使用list的函数中解释了(l:t:k) ,也许是一种编写此简单delete函数的方法,假设该元素位于列表中,则可以所需元素的首次出现并将其删除,并返回新列表。

    delete :: Eq b => b -> [b] -> [b]
    delete r (l:k)
        | inside r k = [l]:delete(r k)
        | otherwise = k

The pattern l:k does three things: 模式l:k做三件事:

  1. It tells you the list is not empty if the pattern matches 如果模式匹配,它会告诉您列表不为空
  2. It binds l to the first element of the list 它将l绑定到列表的第一个元素
  3. It binds k to the rest of the list. 它将k绑定到列表的其余部分。

In the event you have a non-empty list, you want to compare your term r to l . 如果您有一个非空列表,则想将项rl进行比较。 If they are equal, you just return the rest of the list. 如果它们相等,则只返回列表的其余部分。 Otherwise, you put l back on the list that results from the recursive call. 否则,将l放回递归调用产生的列表中。

If the list is empty, you need to handle that as well, by just returning an empty list. 如果列表空,则还需要通过返回一个空列表来处理。 (Deleting r from [] trivially produces [] .) (从[]删除r会产生[] 。)

delete :: Eq b => b -> [b] -> [b]
delete r (h:t) | r == h = t   -- h for head, t for tail
               | otherwise = h : delete r t
delete _ [] = []   -- base case

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

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