我有一份清单,让我们说:
import Data.List
xs = [[1,2], [1,2,3], [2,3]]
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我想获得包含最多项目的内部列表[1,2,3].
我试图使用maximumBy功能从Data.List库:
maximumBy (compare `on` length) xs
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误: not in scope 'on'
任何人都可以告诉我出了什么问题,或者你有更好的方法来获取清单吗?
我有一个家庭作业,我完成了另外一个问题(见标题)
对于我的生活,我无法弄明白......所以我开始认为这是一个棘手的问题.
我将提交的当前答案是:
L1 = {a^n b^n: n>=1} is deterministic. And the reverse,
L2 = {b^n a^n: n>=1} is also deterministic.
Run Code Online (Sandbox Code Playgroud)
但是,由于所有确定性语言都是非确定性语言的子集,因此L2可以被认为是非确定性的.
另一方面,我试图做的唯一其他例子是:
L3= {{a,b}a}
Run Code Online (Sandbox Code Playgroud)
这似乎是可能的,因为前向存在非确定性,因为输入可以是a或b,只要其后跟a即可.
反之则存在决定论,因为它只接受'a'.但是,它引入了新的非确定性,因为第二个输入可以是a或b.
任何帮助/指导都会很棒.
我正在尝试构建一个小型的haskell应用程序,它将把几个关键短语从英语翻译成法语.
首先,我有一个有序的字符串对列表,代表英语单词/短语,后跟法语翻译:
icards = [("the", "le"),("savage", "violent"),("work", "travail"),
("wild", "sauvage"),("chance", "occasion"),("than a", "qu'un")...]
Run Code Online (Sandbox Code Playgroud)
接下来我有一个新数据:
data Entry = Entry {wrd, def :: String, len :: Int, phr :: Bool}
deriving Show
Run Code Online (Sandbox Code Playgroud)
然后我使用icards填充Entrys列表:
entries :: [Entry]
entries = map (\(x, y) -> Entry x y (length x) (' ' `elem` x)) icards
Run Code Online (Sandbox Code Playgroud)
为简单起见,我创建了一个名为Run的[Entry]新类型.
现在,我想根据英文单词中的字符数创建一个哈希表.稍后将使用它来加速搜索.所以我想创建一个名为runs的函数:
runs :: [Run]
runs = --This will run through the entries and return a new [Entry] that has all of the
words of the same length grouped together. …Run Code Online (Sandbox Code Playgroud) 我有一个列表列表,以及一个返回包含最多项目的列表的函数:
extract ::[Plateau]->Plateau
extract(x:xs)
|x==maximumBy(compare `on` length)(x:xs)=x
|otherwise = extract(xs)
extract []=[""]
Run Code Online (Sandbox Code Playgroud)
现在我需要一个函数来获取相同的函数[Plateau]并返回一个[Plateau]删除了之前最大的函数:
prune::[Plateau]->[Plateau]
prune(x:xs)
|x < (maximumBy(compare `on` length)(x:xs)=x : prune (xs)
|x>=maximumBy(compare `on` length)(x:xs)=[]
prune [] = []
Run Code Online (Sandbox Code Playgroud)
我也打电话给修剪,sortBy以确保最大的列表是最后一个:
(extract . prune) (sortBy(compare `on` length)(plateaus))
Run Code Online (Sandbox Code Playgroud)
这开始正常工作.我的列表plateaus看起来像:
plateaus = ["01000"], ["01010", "11010", "10010"] ["00101"], ["01101", "01001"]]
Run Code Online (Sandbox Code Playgroud)
在这里排序:
[["01000"], ["00101"], ["01101", "01001"], ["01010", "11010", "10010"]]
Run Code Online (Sandbox Code Playgroud)
现在,我的函数prune正在返回一个列表
[["01000"], ["00101"]]
Run Code Online (Sandbox Code Playgroud)
这告诉我,由于某种原因Haskell认为
["01101", "01001"] >= ["01010", "11010", "10010"]
Run Code Online (Sandbox Code Playgroud)
当我可以清楚地看到2> = 3不是真的.
为什么是这样?
问题在标题中:
我已经收集到了Big-Oh
O(n 3).
因为那将代表多项式的最高程度.最糟糕的情况是时间复杂性.
通过控制剂量Big-Omega意味着最低程度?即
Ω(n 2)
如果是这样的话,我们怎能证明无视第三学位?
谢谢