Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting a simple Haskell HsLua example to work
    primarykey
    data
    text
    <p>The HsLua examples on the <a href="http://www.haskell.org/haskellwiki/HsLua" rel="nofollow">Haskell Wiki</a> are broken (dostring and dofile are not defined). Looks like the API has changed since the examples were written.</p> <p>However I have been trying to modify the examples to match the current API and not getting much success. Here's a program that <em>should</em> really work!</p> <pre><code>main = do l &lt;- Lua.newstate Lua.openlibs l Lua.loadfile l "configfile.lua" [name,pwd] &lt;- Lua.callfunc l "getuserpwd" "mail.google.com" putStrLn name Lua.close l </code></pre> <p>However, this doesn't even compile, giving me this strange error message -</p> <pre><code>No instance for (Lua.StackValue [String]) arising from a use of `Lua.callfunc' Possible fix: add an instance declaration for (Lua.StackValue [String]) In a stmt of a 'do' expression: [name, pwd] &lt;- Lua.callfunc l "getuserpwd" "mail.google.com" In the expression: do { l &lt;- Lua.newstate; Lua.openlibs l; Lua.loadfile l "configfile.lua"; [name, pwd] &lt;- Lua.callfunc l "getuserpwd" "mail.google.com"; .... } In an equation for `main': main = do { l &lt;- Lua.newstate; Lua.openlibs l; Lua.loadfile l "configfile.lua"; .... } </code></pre> <p>While the content of the file <code>configfile.lua</code> probably doesn't matter (because the haskell code doesn't even compile), it is as below (same as on the wiki page) -</p> <pre><code>function getuserpwd (site) local cookies = { ["www.ibm.com"] = {"joe", "secret"} , ["www.sun.com"] = {"hoe", "another secret"} } if cookies[site] then return cookies[site] elseif site:match("[.]google[.]com$") then return {"boss", "boss"} else return { os.getenv("USER") or "God" , os.getenv("PWD") or "dontdisturb" } end end </code></pre> <p>Could someone please provide me with a working example of Haskell->Lua and Lua->Haskell calls?</p> <p><strong>Edit</strong></p> <p>Okay I changed the type of the return value to a String (from the earlier array of String), and that program <em>does</em> compile! However it now fails at runtime. Here's the modified program -</p> <pre><code>main = do l &lt;- Lua.newstate Lua.openlibs l Lua.loadfile l "configfile.lua" Lua.callfunc l "getuserpwd" "mail.google.com" &gt;&gt;= putStrLn Lua.close l </code></pre> <p>And here's configfile.lua -</p> <pre><code>function getuserpwd (site) return "boss" end </code></pre> <p>And the runtime error message is as follows -</p> <pre><code>**** Exception: user error (attempt to call a nil value) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload