User-defined functions

Questions and problems about using polymake go here.
Blissful1911
Posts: 5
Joined: 20 May 2025, 21:31

User-defined functions

Postby Blissful1911 » 13 Jun 2025, 22:08

Hello! I am new to polymake and I do not understand how to define a function. I tried:

Code: Select all

function double($) { return (2*$_); }
and I got:

Code: Select all

polymake: ERROR: syntax error at input line 1, near "; }" Execution of input aborted due to compilation errors.
Can someone please let me know how to do this?

User avatar
gawrilow
Main Author
Posts: 434
Joined: 25 Dec 2010, 17:40

Re: User-defined functions

Postby gawrilow » 14 Jun 2025, 09:20

This way of defining functions is specific to rule files, which are transformed to valid perl when loaded at the start. The input in the interactive shell and scripts are not transformed, they have to be written according to perl standard. There are few extensions allowed like use of type templates, but otherwise it should be pretty much normal perl code.
If you want to introduce a subroutine, just use the `sub` keyword.

Blissful1911
Posts: 5
Joined: 20 May 2025, 21:31

Re: User-defined functions

Postby Blissful1911 » 15 Jun 2025, 00:27

Thank you for the quick response and for the clarification!

I had previously tried normal perl code but I could not make it work due to another issue with my code. The issue was that I had comment lines starting with # in the code of my function; after removing the comment lines the code worked.

This leads me to another question: how can I put comments in my polymake code?

User avatar
gawrilow
Main Author
Posts: 434
Joined: 25 Dec 2010, 17:40

Re: User-defined functions

Postby gawrilow » 16 Jun 2025, 14:57

As you can see in various polymake rule and script files, the comments are written in the usual fashion, starting with #.
Maybe you can post a minimal example of your input which is rejected by polymake, together with the error message you are receiving?

Blissful1911
Posts: 5
Joined: 20 May 2025, 21:31

Re: User-defined functions

Postby Blissful1911 » 18 Jun 2025, 15:52

Well, I am stumped! After cleaning up my code, I reintroduced comment lines preceded by # and everything appears to be working fine. This suggests I had something else that was causing issues, but I do not have the previous code anymore so I cannot say what exactly. Apologies, and thank you for your patience in addressing these basic questions.


Return to “Helpdesk”