Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't define variables in function #9

Open
bredelings opened this issue Jan 24, 2019 · 2 comments
Open

Can't define variables in function #9

bredelings opened this issue Jan 24, 2019 · 2 comments

Comments

@bredelings
Copy link
Contributor

It looks like if you define variables in a function, then they can't be referenced outside the function. This makes code like the following not work:

function Integer[] cumsum(Integer[] xs) {
         acc = 0
         for (ix in 1:3) {
             acc = acc + xs[ix]
             ys[ix] = acc
         }
         return ys
}

While it would be ideal if such things would work, it might be OK to just document what you can and cannot expect from functions.

@bredelings
Copy link
Contributor Author

Example:

function Integer[] cumsum(Integer[] xs) {
         acc = 0
         for (ix in 1:3) {
             acc = acc + xs[ix]
             ys[ix] = acc
         }
         return ys
}

for (ix in 1:3) {
    z1[ix] = rpois(1, 1)
    z2[ix] = ix
    z3[ix] = 0
}

print(z1)
print(z2)
print(z3)

x = [1,2,3]

acc = 0
for (ix in 1:3) {
    acc = acc + x[ix]
    y1[ix] = acc
}
print(y1)

y2 = cumsum(x)
print(y2)

Output looks like:

> source("rb-command.Rev")
   Processing file "rb-command.Rev"
1,2,0
1,2,3
0,0,0
1,3,6
   Missing Variable:	Variable ys[1] does not exist
   Error:	Problem processing line 29 in file "rb-command.Rev"
> 

@wrightaprilm
Copy link
Collaborator

This is sort of odd, because I get the same error if I save rb-command.Rev in a regular text editor and try running it with the eb executable at the cmd line. But if I run cell-by-cell in a Jupyter notebook, I get the expected output. But then if I run it line-by-line in the revbayes exectuteable, I still get the Variable ys[1] does not exist. I'm not sure what's going on here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants