Bad function arity and warning: function hello_world/0 is unused - erlang

I just started learning Erlang so I coded this as new.erl:
-module(new).
-export([hello_world]/0).
hello_world()->io:fwrite("Hello\n").
So the function name is hello_world and it holds Hello as string.
But whenever I want to run this, I get this result:
new.erl:2: bad function arity
new.erl:3: Warning: function hello_world/0 is unused
error
So how to solve this problem, what's going wrong here ?

You wrote:
-export( [ hello_world ]/0 )
It should be:
-export( [ hello_world/0 ] ).
export requires a list, where each element in the list is the function name followed by a slash followed by the arity of the function. A list does not have an arity, which is what you wrote.
Note that when you are just writing test code, it is easier to use:
-compile(export_all).
which will export all functions defined in your module.

Related

How to modify a specific derivation option in nix package manager

I want to modify the definition of a Nix derivation (emacs macport). I wish to change the configureFlag value and "--with-mac-metal" to it.
I have tried the following with no luck:
emacsMacport.overrideDerivation
(old: {
configureFlags = [
"LDFLAGS=-L${ncurses.out}/lib"
"--with-xml2=yes"
"--with-gnutls=yes"
"--with-mac"
"--with-modules"
"--enable-mac-app=$$out/Applications"
"--with-mac-metal"
];
})
I am using home-manager and nix-darwin, and I get the following exception:
error: A definition for option `home-manager.users.ashk.home.packages.[definition 16-entry 3]' is not of type `package'. Definition values:
- In `/nix/store/mkcwa9i9brbxf81a01whhy53yzk87c9d-source/modules/hosts/zebra/home.nix': <function>
(use '--show-trace' to show detailed location information)
You need to parenthesize function applications when they're in a list literal.
It's weird.
You'll probably never get used to this, judging from my own experience using Nix extensively for years.

How to add to a Lua DissectorTable?

I'm writing a Lua dissector for Wireshark for a complex protocol. The protocol has a message header that includes a msgType field. I want to write a subdissector for each message type, with each subdissector stored in a separate source file.
My top-level script is general.lua which dissects the message header and creates the dissector table:
DissectorTable.new("myProtocol.Message")
dofile(DATA_DIR.."cplane.lua")
cplane.lua is a subdissector for message type 'cplane' and includes the code:
my_dissector_table = DissectorTable.get("myProtocol.Message")
my_dissector_table:add(0x02, myProtocol_cplane_proto)
Both scripts are in the same subdirectory of Wireshark's plugins directory.
When I load the plugins I get error:
Lua: Error during loading:
[string "C:\Program Files (x86)\Wireshark\plugins\2.4...."]:9: bad argument
#1 to 'get' (DissectorTable_get: no such dissector_table)
Lua: Error during loading:
[string "C:\Program Files (x86)\Wireshark\plugins\2.4...."]:170: bad
argument #1 to 'dofile' (dofile: file does not exist)
How can I fix this? Is the problem to do with the loading order of the scripts? Is the dofile() call necessary?
It is not necessary to use dofile as all scripts in the plugins directory are loaded. The order of loading is however not fixed (at least, it is not documented to be fixed). Currently Lua plugins are loaded after other dissectors, so trying to lookup dissector tables in the "global scope" will only work for built-in dissectors, such as tcp.port:
local myproto = Proto("myproto", "My Protocol")
function myproto.dissector(tvb, pinfo, tree)
...
end
-- Register with a built-in dissector table
DissectorTable.get("tcp.port"):add(1234, myproto)
For registering with custom dissector tables, this registration has to be deferred. In C dissectors, you would put the registration in proto_reg_handoff_PROTOABBREV (where PROTOABBREV should be substituted accordingly), but in Lua there is no such function.
The closest you can get is the "init" routine (a property of the Proto class, proto.init). These are called when a capture file is opened, before dissecting any packets. Example:
function myproto.init()
DissectorTable.get("your-custom-table"):add(1234, myproto)
end
Lua: Error during loading: [string "C:\Program Files
(x86)\Wireshark\plugins\2.4...."]:9: bad argument
#1 to 'get' (DissectorTable_get: no such dissector_table)
Answer: This error means that Dissector table is not found. Reason could be that the path is not correct, or the sequence of the file execution.
Lua: Error during loading: [string "C:\Program Files
(x86)\Wireshark\plugins\2.4...."]:170: bad argument #1 to 'dofile'
(dofile: file does not exist)
Answer: For me this error is gone by entering the exactly correct path

Using records in functions

I was just playing with records, and am stuck trying to write a function that works with records. I think the problem is how to read the record definition into my module file.
The reocords.hrl file:
% Modeling a todo list
-record(todo, {status=reminder, who=joe, text}).
And then the use_records.erl file:
%% Use the records defined in "records.hrl"
-module(use_records).
-export([todo_to_tuple/1]).
rr("records.hrl").
todo_to_tuple(#todo{who=W, text=T} = R) -> {W, T}.
When I try to compile it, I get:
24> c(use_records).
use_records.erl:5: variable 'T' is unbound
use_records.erl:5: variable 'W' is unbound
use_records.erl:5: record todo undefined
use_records.erl:5: Warning: variable 'R' is unused
error
The error is the same if I remove the rr("records.hrl") line. So I suppose the real issue is being able to read the record definition (or not?). Please help!
rr/1 is used include record definition only in the shell.
In order to include record definition in your code use:
-include("records.hrl")

lua - invalid argument type

I am a newbie to Lua. Currently getting the following error message:
invalid argument type for argument -model (should be the model checkpoint
to use for sampling)
Usage: [options] <model>
I am sure it is something pretty easy to solve, but cannot manage to find the solution.
The 'model' is a file lm_checkpoint_epoch50.00_2.7196.t7, which is in the directory
/home/ubuntu/xxx/nn/cv
I am running the program from the parent directory (/home/ubuntu/xxx/nn)
I have tried out the following options to run the program (from one directory above the one the model is saved):
th sample.lua - model lm_chelm_checkpoint_epoch50.00_2.5344.t7
th sample.lua lm_chelm_checkpoint_epoch50.00_2.5344.t7
th sample.lua /cv/lm_chelm_checkpoint_epoch50.00_2.5344.t7
th sample.lua - /cv/model lm_chelm_checkpoint_epoch50.00_2.5344.t7
Also, the program has a torch.CmdLine() object where :argument equals '/cv/lm_checkpoint_epoch50.00_2.7196.t7'. The program prints the parameters, so that you see the following output on the screen:
Options
<model> /cv/lm_checkpoint_epoch50.00_2.7196.t7
so it finds a value for argument 'model', which is picked up from the .lua file, not the parameter in the command line. This file is a valid mode.
Pretty lost, hope someone relates to this issue. Thanks.
found the issue - it was a bug as smhx suggested. I inadvertently changed the source code from:
require 'torch'
cmd = torch.CmdLine()
cmd:argument('-model','model checkpoint to use for sampling')
Note that there is no argument in the source code. To:
cmd:argument('-model','/cv/model lm_chelm_checkpoint_epoch50.00_2.5344.t7'
'model checkpoint to use for sampling')
So the argument must be passed through the command line, not the source code. With parameters, it is different - you can include them in the source code.
So if I change back the source code and run the following from the command line:
th sample.lua cv/lm_chelm_checkpoint_epoch50.00_2.5344.t7
it works.

Elixir io_lib call to erlang

io_lib:fread("~d/~d/~d", "2013/03/03").
Above code works in erlang so ideally in elixir below code should work
:io_lib.fread("~d/~d/~d", "2013/03/03")
but it generates error " no function clause matching "
After inspecting found that elixir makes call to module like
:io_lib_fread.fread("~d/~d/~d", "2013/03/03", 0, [])
A double quote in erlang "char list" translates to single quotes in Elixir 'char list'.

Resources