为什么不允许这样做?
type Foo() =
static member Bar() = ()
let inline bar<^a>() = //ERROR: unexpected infix operator in pattern
(^a : (static member Bar : unit -> unit)())
//Hypothetical usage: let _ = bar<Foo>()
Run Code Online (Sandbox Code Playgroud)
......但是这样可以吗?
type Foo() =
static member Bar() = new Foo()
let inline bar() : ^a =
(^a : (static member Bar : unit -> ^a)())
let x : Foo = bar()
Run Code Online (Sandbox Code Playgroud)
具有静态解析类型参数的函数是否需要返回已解析类型的实例?
考虑具有n个节点的二叉树.有多少种不同的二叉树结构?
我尝试过类似的东西:
n number of different structure:
1 1
2 4
3 16
Run Code Online (Sandbox Code Playgroud)
对于n> 1,4(n-1)也是如此; 1 = n == 1?
我想为加泰罗尼亚语编写代码.加泰罗尼亚语数字定义如下:
C(n) = 2n C n/(n+1).但是,(2n C n)我没有使用计算,而是想使用以下事实计算加泰罗尼亚数字:
Catalan(n) =
2n! /n! * n! * (n+1)
Catalan(n+1) =
2*(n+1)
--------------------------- =
(n+1)! * (n+1)! * ((n+1)+1)
(2n+2) * (2n+1) * 2n!
------------------------------- =
(n+1) * n! * (n+1) * n! * (n+2)
(2n+2) * (2n+1) * 2n!
----------------------------------- =
(n+1) * (n+2) * n! * n! * (n+1)
(2n+2) * (2n+1)
--------------- * Catalan(n)
(n+1) * (n+2)
Run Code Online (Sandbox Code Playgroud)
现在利用上述事实,这是我的以下代码:
int catalan(int n)
{
if …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 prolog 中编写一些 dcg 语法,它将描述
a^nb^n n>=0
"",ab,aabb,aaabbb itd
我写的都是
s --> slowo.
slowo --> [a],slowo,[b],!.
slowo --> [].
Run Code Online (Sandbox Code Playgroud)
只要我想做的只是检查单词是否正确,它就很好,但是 dcg 语法应该如何在 prolog 中查找,以便?-phrase(s,X)从我的语言中生成所有单词?
我尝试做一个尾递归函数,它将计算列表的元素,遵循规则,使用一个累加器,但当我像这样运行它:
lstcountr [1..98765432];;
Run Code Online (Sandbox Code Playgroud)
我明白了:
System.OutOfMemoryException:抛出了类型'System.OutOfMemoryException'的异常.
这是我的函数(我认为是尾递归/高效):
let lstcountr ls =
let rec loop ls total =
match ls with
| [] -> total
| hd::tl -> loop tl total+1I
loop ls 0I
Run Code Online (Sandbox Code Playgroud)
这可以做得更好吗?
问题是关于.net中数组的分配.我有一个示例程序,其中我可以得到的最大数组是长度.我将长度增加到+1它给出了outofMemory异常.但如果我保持长度并删除评论,我可以分配2个不同的大数组.两个数组的.net允许对象大小为2 gb,总内存也小于虚拟内存.有人可以提出任何想法吗?
class Program
{
static int length = 203423225;
static double[] d = new double[length];
//static int[] i = new int[15000000];
static void Main(string[] args)
{
Console.WriteLine((sizeof(double)*(double)length)/(1024*1024));
Console.WriteLine(d.Length);
//Console.WriteLine(i.Length);
Console.WriteLine(Process.GetCurrentProcess().VirtualMemorySize64.ToString());
}
}
Run Code Online (Sandbox Code Playgroud) 当我做这样的断言时:
assert(-color(red)).
Run Code Online (Sandbox Code Playgroud)
它给了我错误:
ERROR: assert/1: No permission to modify static procedure `(-)/1'
Run Code Online (Sandbox Code Playgroud)
所以我将 -color 更改为动态:
dynamic -color/4.
Run Code Online (Sandbox Code Playgroud)
现在它给了我错误:
ERROR: dynamic/1: Type error: `atom' expected, found `-color'
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我跟随现实世界的哈克尔,第2章有一个练习.
我的解决方案是
lastButOne xs = if null xs || null (tail xs)
then []
else if null (tail (tail xs))
then head xs
else lastButOne (tail xs)
Run Code Online (Sandbox Code Playgroud)
但除了[]之外它不起作用,并产生这样的错误.
*Main> lastButOne []
[]
*Main> lastButOne [1, 2]
<interactive>:5:13:
No instance for (Num [a0]) arising from the literal `1'
Possible fix: add an instance declaration for (Num [a0])
In the expression: 1
In the first argument of `lastButOne', namely `[1, 2]'
In the expression: lastButOne [1, 2]
Run Code Online (Sandbox Code Playgroud)
我是一个相当新手,不明白这些神秘的错误信息.有任何想法吗?
我正在尝试在Fsharp中实现一个基本的红/黑树,基于公共代码(http://en.wikibooks.org/wiki/F_Sharp_Programming/Advanced_Data_Structures)
但是我在编译时遇到了最终签名的问题:
type 'a BinaryTree(inner : 'a tree) =
member this.head = Tree.head inner
member this.left = BinaryTree(Tree.left inner)
member this.right = BinaryTree(Tree.right inner)
member this.exists item = Tree.exists item inner
member this.insert item = BinaryTree(Tree.insert item inner)
member this.print() = Tree.print 0 inner
static member empty = BinaryTree<'a>(E)
Run Code Online (Sandbox Code Playgroud)
具体来说,它给了我一个错误,"一个类型参数缺少一个约束'a when:comparison'"但是我尝试添加它的每件事都没有用.我错过了什么?
如何防范DateTime.TryParseExact(并在可能的情况下获得解析的值)?以下代码不起作用.
[<EntryPoint>]
let main args =
let argList = args |> List.ofSeq
match argList with
| "aaa" :: [] -> aaa.main "aaa"
| "bbb" :: [] -> bbb.main "bbb"
| "ccc" :: yyyymm :: [] when DateTime.TryParseExact
(yyyymm, "yyyyMM", CultureInfo.InvariantCulture, DateTimeStyles.None)->
ccc.main "ccc" yyyymm
Run Code Online (Sandbox Code Playgroud)