小编Guy*_*der的帖子

类型(单位 - >单位)的函数是否可以在F#中静态解析类型参数?

为什么不允许这样做?

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)

具有静态解析类型参数的函数是否需要返回已解析类型的实例?

f# inline

1
推荐指数
1
解决办法
383
查看次数

对于二叉树,假设有n个节点,我可以构造多少个不同的结构?

考虑具有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?

binary-tree catalan

1
推荐指数
2
解决办法
8862
查看次数

我的加泰罗尼亚数字逻辑出了什么问题?

我想为加泰罗尼亚语编写代码.加泰罗尼亚语数字定义如下:

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)

c algorithm math catalan

1
推荐指数
1
解决办法
3432
查看次数

Prolog dcg 从语言中生成所有单词

我正在尝试在 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)从我的语言中生成所有单词?

prolog left-recursion dcg

1
推荐指数
1
解决办法
1074
查看次数

tail递归/高效函数来计算列表项(不使用List.count/Seq.count)

我尝试做一个尾递归函数,它将计算列表的元素,遵循规则,使用一个累加器,但当我像这样运行它:

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)

这可以做得更好吗?

f# tail-recursion

1
推荐指数
2
解决办法
737
查看次数

.NET中的数组分配

问题是关于.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)

.net c# gcallowverylargeobjects

1
推荐指数
1
解决办法
2908
查看次数

无权修改静态过程`(-)/1'

当我做这样的断言时:

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)

有任何想法吗?

prolog

1
推荐指数
1
解决办法
6707
查看次数

嵌套 - 如果在Haskell中

我跟随现实世界的哈克尔,第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)

我是一个相当新手,不明白这些神秘的错误信息.有任何想法吗?

haskell

1
推荐指数
1
解决办法
210
查看次数

来自wikibooks.org的二叉树比较的F#/ FSharp通用类型

我正在尝试在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'"但是我尝试添加它的每件事都没有用.我错过了什么?

generics f#

1
推荐指数
1
解决办法
144
查看次数

模式匹配守卫与DateTime.TryParseExact?

如何防范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)

f# guard-clause

1
推荐指数
1
解决办法
491
查看次数