Why is Compile[] failing with message Compile::extscalar? [closed]

g = RandomGraph[{1000, 6000}];
NumberOfVertices = VertexCount @ g;
A = AdjacencyMatrix @ g;
ele = Flatten[A];
cnt = Length /@ A;
dsp = FoldList[Plus, 1, cnt];
fun = Compile[{{ele, _Integer, 1}, {cnt, _Integer, 1}, {dsp, _Integer,
1}, {n, _Integer}, {ccc, _Integer, 1}},
NestWhile[
Function[{cc},
Fold[Function[{c, v},
ReplacePart[c,
Module[{i,
com = Complement[Range[n],
c[[Take[ele, {dsp[[v]], dsp[[v]] + cnt[[v]] - 1}]]]]},
RandomChoice[Append[com, c[[v]]]]], v]], cc,
RandomSample[Range[NumberOfVertices], NumberOfVertices]]], ccc,
Max[#] > n &]];
fun[ele, cnt, dsp, 100, Range[NumberOfVertices]]


Its giving me the following warnings:

Compile::extscalar: Function[{cc},Fold[Function[{c,v},ReplacePart[c,Module[{i,com=Complement[<<2>>]},RandomChoice[Append[com,Part[<<2>>]]]],v]],cc,RandomSample[Range[NumberOfVertices],NumberOfVertices]]] cannot be compiled and will be evaluated externally. The result is assumed to be of type Real. >>
Compile::extscalar: Max[#1]>n& cannot be compiled and will be evaluated externally. The result is assumed to be of type Real. >>

-

closed as off-topic by István Zachar, MarcoB, Louis, Yves Klett, m_goldbergJun 17 at 20:41

This question appears to be off-topic. The users who voted to close gave these specific reasons:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – MarcoB, m_goldberg
• "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – István Zachar, Louis, Yves Klett
If this question can be reworded to fit the rules in the help center, please edit the question.

Why is NumberOfVertices not defined in your code? – J. M. Nov 11 '12 at 3:10
@J.M. sorry, just added it as a global but it could be any positive integer. – Tom Wellington Nov 11 '12 at 6:42
I'm not sure that this question is answerable as such, because there's really no specific reason; Compile is just not very effective for functional-style code, especially with nested scoping constructs. Essentially, the style that's most easily understood by the compiler is the polar opposite of what's needed for top-level code. You'll probably have more success if you rewrite in a more procedural style. – Oleksandr R. Nov 11 '12 at 10:03
One problem is that ele is a sparse array and is not among the supported types for compile. NestWhile isn't amongst the results for CompilerFunctions[] and I'm not certain but ccc might also be causing problems, perhaps to the function with cc as it's parameter. – image_doctor Nov 11 '12 at 11:26
@image_doctor yes, exactly. There are multiple issues here, each of them sufficient to prevent compilation. Fixing them all will require modifying the existing code to such an extent that I thought it best just to suggest a rewrite, since it's obvious just by looking that the compiler is going to choke on something like this in its present form. – Oleksandr R. Nov 11 '12 at 12:18