Re: programming help!



wwajm@xxxxxxxxx wrote:
I mean the element of adjlist[i] is null. I did what you said and
instead of boolean I'm just using 0 and 1. 0 if false, 1 if true. and
then I return 0 or 1 depending on my result. I'm really stuck with
lists now, I understand that they can grow like a stack and a Que, but
can I still access them as an Array? My main problem right now is that
inside the following function, my value 'p' always ends up being
&null.

You can access existing elements of a list as an array, but attempts
to access outside the bounds of the list will fail. If you want to
grow an array you have to do so explicitly (e.g. with put(), push(),
etc.). This looks like what's biting you. Since the array starts
out with 0 elements any attempt to reference an element, as in
adjlist[i], is going to fail. So...

procedure addEdge(i, r, j) #i = tag1, r = ratio, j = tag2

newNode := node(r,j,adjlist[i])

If adjlist is the empty list, the reference to adjlist[i] will
fail - and the call to node() [and the subsequent assignment]
will not take place. Why are to trying to pass to node
an array element value that doesn't exist yet? Since it
doesn't exist, I don't see how node() can do anything with it!
[Assuming I'm right that you're starting with adjlist := [].]

put(adjlist, newNode)
#adjlist[i] := newNode <<<I also tried using that. however I
got the same null result.

newNode is null, since node() never got called because of the
failure of reference to the non-existent element i of adjlist.
So the put(adjlist, newNode) is inserting a new element into
adjust list, but with a null value.

(The commented out version is failing because the left-hand-side
"adjlist[i]" fails, preventing the assignment from taking place,
but even if it didn't, having newNode be null would be a problem.)

end

# the dfs procedure returns a 1 if true, or a 0 if false. the line "if
p===&null then write("null :(")" will always print, and that means
that the while loop will never execute. I can't tell if its the
initializing of the adjlist itself or the actual dfs function that has
the problem. adjlist was initiated w/ " adjlist := [] " and is a
global variable. procedure dfs(i, r, j)

if i = j then return 1
else {
visited[i] := 1

p := adjlist[i]

if p===&null then write("null :(")

while \p do {
if ( visited[p.target] = 0 ) then {
if (dfs(p.target, ratioMultiply
(p.rate, r), j) = 1) then
return 1
else
r := ratioDivide(p.rate, r)
}

p := p.nextNode

}#end while
return 0

}#end else
end

Ok, aside from possibly referencing non-existent array elements, this
looks ok - though not very 'Icony'. Normally, one would right
dfs to succeed or fail directly, which eliminates the need to
later test the result for 0 or 1. That is, every where you have
"return 0", you could have "fail". Then the use of dfs(...) could
be:

if dfs(...) then

instead of

if 1 = dfs(...) then

Boolean values are rarely really needed in Icon and almost always a
way to spot code that was more or less directly translated from
some other language. That's not necessarily bad if the goal is
to simply get something running in Icon, but if the goal is to
actually learn how to really take advantage of the power of Icon
it's heading in the wrong direction.

finally, why is it that some lines in Icon end with a semicolon and
others do not? its driving me crazy!

All 'statements' in Icon (not lines) end in semicolons. Is just that
the Icon translator is pretty good at inserting semicolons when
statements end at line boundaries. What drives you crazy is that
it's easy to write statements that 'look like' they end at the
end of a line, but don't. For example:

x := y
/x(a,b,c)

is seen as a single statement: x := y/x(a,b,c)
which is perfectly legitimate Icon code, but almost certainly
not what the programmer wanted. Here, if you really meant that
to be two different statements, you'd have to force the semicolon
to make it clear:

x := y;
/x(a,b,c)



--
Steve Wampler -- swampler@xxxxxxxx
The gods that smiled on your birth are now laughing out loud.
.



Relevant Pages

  • Re: Dynamic Casting with Generics: Type Erasure Problems
    ... what I'm looking for is WHY DOES IT NEED TO FAIL? ... create an array of type T where you can create a List of type T, ... arrays are reified but Lists are not. ... is quite a reasonable question, because the answer will usually teach ...
    (comp.lang.java.programmer)
  • Re: Dynamic Casting with Generics: Type Erasure Problems
    ... what I'm looking for is WHY DOES IT NEED TO FAIL? ... And generally, when there's a limit to what generics can do, there is a good reason for it, e.g. not being able to create an array of type T where you can create a List of type T, because arrays are reified but Lists are not. ... is quite a reasonable question, because the answer will usually teach you something. ...
    (comp.lang.java.programmer)
  • Re: garbage collection problem in large linked lists
    ... Instead all buckets are allocated at once in an array. ... VG.net, which must be very scalable, but only for lists which would normally ... Linked lists require pointer dereferencing in order to traverse. ... When you run out of space, you allocate a new smaller array. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Dict sharing vs. duplication
    ... array to enforce unique keys and I use lists to enforce order. ... API in a page or two of Tcl code, it isn't so much a missing feature ... OpenACS database API which creates a new specialized data structure, ...
    (comp.lang.tcl)
  • Re: Translating python to scheme
    ... Scheme has mutation, and there are reasons to use it. ... confusing when considering a 2d array. ... in other languages: Lisp Lists and Vectors. ...
    (comp.lang.scheme)