- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图找出哪个整数列表更大。[1;2;3]
的表示实际上是 123
.
所以给定两个整数列表,我想比较每个元素以确定最终哪个数字更大。返回 1
如果 list1 > list2
, -1
如果 list1 < list2
, 和 0
如果他们是平等的。
这是我到目前为止所拥有的,但是当我尝试运行它时,我得到了错误,我不明白为什么。我还被要求尽可能以尾递归形式进行所有重复。这是正确的尾递归吗?
let rec compare' list1 list2 currentOrder = match (list1, list2) with
| [],[] -> 0
| list1, [] -> 1
| [], list2 -> -1
| car1::cdr1, car2::cdr2 -> (*car1 contains the current element and cdr1 contains the rest of the list. Ex car1 = [1], cdr2 = [2;3]*)
let nextCompare =
(
if cdr1 = [] && cdr2 = []
then 0
else 1
)
in
(
if (nextCompare != 0)(*To check if we are not at the end of the list*)
then ( (*Compare the cars to determine the currentOrder and recursively call with the next number*)
if car1 > car2
then (compare' (cdr1 cdr2 1))
else if car1 < car2
then (compare' (cdr1 cdr2 -1))
else (compare' cdr1 cdr2 currentOrder)
)
else (*we are at the end of the list so we want to return the current order*)
( (*Still need to check in case there is only 1 digit number, because we did not update currentOrder for it to be returned*)
if car1 > car2
then 1
else if car1 < car2
then -1
else currentOrder
)
)
Printf.printf (compare' [1;2;3] [3;2;1] 0)
最佳答案
您遇到的第一个问题是一些函数应用程序(例如 (compare' (cdr1 cdr2 1))
)的参数周围有一些额外的括号,它告诉 OCaml 您想调用 cdr1
带有参数 cdr2
和 1
,然后将结果应用到 compare'
.因此编译器会提示你试图使用列表 cdr1
作为一个函数。如果您习惯于使用括号来运行应用程序,这是一个简单的错误。
第二个问题是你有 -1
不带括号,编译器会将其解释为应用 1
到二进制 -
运算符,因此提示其结果是一个函数而不是一个 int,正如你所期望的那样。这种奇怪行为的原因是 OCaml 支持部分函数应用,这会导致一些歧义。消除歧义,您只需将其括在括号中:(-1)
.
最后,您的代码中有相当多的冗余,所以我为您简化了一些。更少的代码也意味着更少的地方可以搞砸,并且可以通过查看来找到搞砸的地方,这在你学习时非常方便:)
let rec compare' list1 list2 currentOrder =
match list1, list2 with
| [], [] -> currentOrder
| _, [] -> 1
| [], _ -> -1
| car1::cdr1, car2::cdr2 ->
let order =
if car1 > car2 then 1
else if car1 < car2 then -1
else currentOrder
in
compare' cdr1 cdr2 order
关于compiler-errors - 确定哪个整数列表更大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46858617/
我正在 Java 上开发 Tic Tac Toe 游戏(eclipse)。在我的计算机上,我的对话框非常小。我一直在努力把它做得更大。我没有任何运气。我希望这里有人能引导我走向正确的方向。下面的代码是
出于辅助功能的目的,我需要使所有可滚动控件(列表、 ListView )的滚动条更宽,滚动条箭头更大。 如何从可滚动控件(例如:TListView)获取滚动条( handle )? 如何使滚动条和箭头
在我的 flutter 应用程序中,我想制作一个圆圈作为这样的背景: 最佳答案 问题主要是当屏幕处于横向时,除非它覆盖整个屏幕,否则你不能在那里放置一个圆圈。 ClipRect 还有另一种方法,如果这
如何在列表中找到最接近的值,这将返回更高的值?示例:[3,7,12,19] 的列表,如果我的值为 8,我如何获得最接近(更大)的值 12?我想要 dart 中的这个逻辑。 最佳答案 只需过滤 List
我是一个绝对的 HTML 和 javascript 初学者,我很确定,这真的很容易实现。我生成了以下 IFRAME: document.write("");Das aktuelle Wetter in
我有点知道为什么压缩 DES 文件时它更大,但谁能给我正确的理由或链接,我似乎找不到任何原因? 谢谢:) 最佳答案 经过适当加密的密文在压缩程序看来非常像一系列随机数。如果没有以明文形式出现的模式,压
假设我有两个表,其中一个(我们称之为 person)有一个名为 type 的字段,该字段存储链接到另一个表的整数(我们称之为types 带有一个名为 id 的字段。什么会更能提高性能? 一个更大的查询
我有一个ListView,它使用GridView来显示DataTable,并且当新行添加到DataTable
我在只出现在 Firefox 中的导航菜单上遇到了一个奇怪的问题(在 IE7、8、9、10 和 Chrome 中工作正常)。菜单上的链接显示为 block ,但它们在 Firefox 中计算得比任何其
我正在使用 javax.mail 并获取邮件收件箱文件夹中的邮件计数。使用https://outlook.office.com/ Folder folderInbox = store.getFolde
我想让地球字形图标更大,以便它覆盖页面的大部分(它是矢量图像)。它不在按钮或任何东西中;它只是一个人。有没有办法做到这一点? 最佳答案 增加 glyphicon 的 font-size
我定制了一个看起来像这样的搜索栏 问题是很难捕获 slider 。所以我想做的是增加拇指的碰撞箱以使其更容易。我尝试了一些东西,但总是弄乱了搜索栏的外观。我有什么想法可以做到这一点吗? 布局.xml
我有一个问题:下载到 Documents 文件夹中的音乐比以前长 2 倍,而且体积更大。比特率也有问题(增加/减少 2 倍)。图片保存得很好,但任何音乐都有问题。也许我需要手动设置音频比特率,但我还没
为什么 div 区域仍然比图像 css 大? /image/WHDU3.jpg 如何设置 div 区域以适合图像? 最佳答案 IMG - 是行内元素,因此
我发现在对文本文件进行排序时非常困惑。不同的算法/应用程序会产生不同的结果,例如,比较两个字符串 str1=";P" 和 str2="-_-" 仅供引用,这里给出了这些字符串中每个字符的 ASCII:
我有一个应用程序已经在 Play 商店中存在多年,我刚刚构建了一个更新。该更新利用了 numberpicker,它的 min-sdk 为 11,并且在 v4 支持库中不适用。我倾向于发布我的新版本并在
你能告诉我如何强制 CSS 使 line-through 属性比元素 width 宽吗? 例如 50 结果看起来像现在如何使线条比元素更宽更明显? 赞 最佳答案 你可以使用 ,这是一种很俗气的方式 &
我想制作一个比应有的大得多的虚拟 Win32 EXE 文件。所以默认情况下,样板 Win32 EXE 文件为 80 KB。我想要一个 5 MB 的空间来测试其他一些实用程序。 第一个想法是添加资源,但
我有一个 UIView,里面有一个更大的 UIImageView。有没有办法隐藏超出 View 大小的内容? CGRect baseFrame = CGRectMake(100, 100, 300,
当我改变缓冲区的大小时,我得到了无法从 BufferedReader 解释的奇怪结果。 我曾强烈期望性能会随着缓冲区大小的增加而逐渐增加, yield 递减设置相当快,此后性能或多或少会持平。但看起来
我是一名优秀的程序员,十分优秀!