如何使用自定义版本的 List:`data List a = Nil |缺点(列表 a)`? [英] How to use custom version of List: `data List a = Nil | Cons a (List a)`?

查看:26
本文介绍了如何使用自定义版本的 List:`data List a = Nil |缺点(列表 a)`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题基于格雷厄姆·赫顿 (Graham Hutton) 所著的 Haskell 编程"一书中 声明类型和类" 一章中的一个示例,第二版.

This question is based on an example from the chapter "Declaring Types and Classes" of the book "Programming in Haskell" by Graham Hutton, second edition.

数据声明为:data List a = Nil |缺点a(列表a)

使用此声明的示例函数是:

The example function that uses this declaration is:

len :: List a -> Int
len Nil = 0
len (Cons _ xs) = 1 + len xs

但无论我尝试过什么,我似乎都无法使用函数len:

But no matter what I've tried, I can't seem to use the function len:

  • len 缺点 1 缺点 2 缺点 3
  • len 1
  • len 缺点
  • len (1)
  • len [1,2]
  • len 缺点 [1]
  • len (1,2)
  • len 缺点 (1,2)
  • len 缺点 1 2
  • len (Cons (1,2))
  • len(缺点 1 2)

我是否遗漏了 lenCons 的任何排列?还是这个例子根本行不通?

Did I miss out any permutation of len and Cons? Or is the example simply unworkable?

推荐答案

您传递给 len 的参数不是 List .列表的末尾有 Nil,所以列表的形式是 Nil, Cons ... Nil, Cons ... (Cons ... Nil), Cons ... (Cons ... (Cons ... Nil)) 等等.所以最终对于每个列表,列表的末尾都标有Nil.Nil 相当于 Haskell 的 [a] 类型的 [] .

The parameters you pass to the len are not Lists. The end of a list has Nil, so lists are of the form Nil, Cons … Nil, Cons … (Cons … Nil), Cons … (Cons … (Cons … Nil)), etc. So eventually for each list, the end of the list is marked with Nil. Nil is the equivalent to [] for Haskell's [a] type.

此外,您不能通过 len Cons 1 Nil 为例,因为那样它将被解释为 ((len Cons) 1) Nil.参数应该是一个列表.通过使用括号,您可以将其写为 len (Cons 1 Nil).

Furthermore you can not pass len Cons 1 Nil for example, since then it will be interpreted as ((len Cons) 1) Nil. The parameter should be a list. By using parenthesis, you can write this as len (Cons 1 Nil).

对于给定的样本数据,您可以将其重写为:

For the given sample data, you thus can rewrite this to:

  • len Cons 1 Cons 2 Cons 3len (Cons 1 (Cons 2 (Cons 3 Nil)))
  • len 1len (Cons 1 Nil)
  • len 缺点len 无
  • len (1)len (Cons 1 Nil)
  • len [1,2]len (Cons 1 (Cons 2 Nil))
  • len Cons [1]len (Cons 1 Nil)
  • len (1,2)len (Cons 1 (Cons 2 Nil))
  • len Cons (1,2)len (Cons 1 (Cons 2 Nil))
  • len Cons 1 2len (Cons 1 (Cons 2 Nil))
  • len (Cons (1,2))len (Cons 1 (Cons 2 Nil))
  • len (Cons 1 2)len (Cons 1 (Cons 2 Nil))

您还可以使用 OverloadedLists [haskell-doc] 扩展以使用列表语法.在这种情况下,您需要实施 IsList 类型类:

You can also make use of the OverloadedLists [haskell-doc] extension to use list syntax instead. In that case, you need to implement the IsList type class:

{-# LANGUAGE TypeFamilies #-}

import GHC.Exts(IsList(..))

data List a = Nil | Cons a (List a)

instance IsList (List a) where
    type Item (List a) = a
    toList Nil = []
    toList (Cons x xs) = x : toList xs
    fromList [] = Nil
    fromList (x:xs) = Cons x (fromList xs)

如果您随后启用 OverloadedLists 扩展,您可以将它们写为列表文字:

If you then enable the OverloadedLists extension, you can write these as list literals:

{-# LANGUAGE OverloadedLists #-}

-- …

main = print (len [1,2])

这篇关于如何使用自定义版本的 List:`data List a = Nil |缺点(列表 a)`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆