Good morning fellow programmers!
i'm working on a project using haskell, and i've wanted to know how to run a haskell function without having to type two lines on ghci, for example
ghci filename.hs function
This can only be done doing:
ghci filename.hs
function
????
I'm looking for something like the main () in C,which runs automatically when you compile the program
Is there something like that?
I've been checking the -e option on ghci, but i cant seem to get it to work!
Thank you very much!
Cheers!
You're probably looking for ghc -e instead:
> echo 'foo x y z = x+y*z' > foo.hs % let's make a foo.hs file
> ghc foo.hs -e 'foo 1 2 3' % call the function in foo.hs
=> 7
Also, note that you can also use the :reload command in ghci. Load the file in ghci, edit, type :reload and test again. Also, if this seems too tedious, you can also define a ghci macro which allows you to reload and test your function at the same time:
> :def test \x -> return (":reload\n" ++ x)
> :test foo 1 2 3
=> Ok, modules loaded: Foo.
7
If you're looking to build real programs instead of quickly testing
your functions, then you'd better read the other answers on writing
main functions.
I assume function has the type IO (). Then you can just let main = function, and use for example runhaskell modulename from the command line. As in C, main is a special function.
To clarify a bit, just in case: If your function is a pure one, i.e. one whose type does not invovle IO, you can't really "run it". I guess it's a simplification to say this, but essentially what GHCi does is to call print function. If you want to mimic this, you can use something like main = print function and use runhaskell. This assumes function's type is an instance of Show.
Related
I would like to apply uncrustify (via beautify in the Atom editor and a config file) to CUDA code. However, I don't know how to tell uncrustify to recognize CUDA kernel calls which have the following structure:
kernelName <<<N,M>>> (arg0,arg1,...);
However, uncrustify has problems with the <<< >>> and applying it gives the following unpleasant result
kernelName << < N, M >> >
(arg0,arg1,...);
I would like to have it look more like a function call and also avoid the formatting of <<< to << <. Ideally, the result would look like
kernelName <<< N, M >>> (arg0,arg1,
...); // line break if argument list is too long
Which arguments can I add to my config.cfg to achieve the above result?
Thank you very much.
Looking through whole documentation of uncrustify, I have found 2 arguments that could influence in your CUDA kernel style:
sp_compare { Ignore, Add, Remove, Force }
Add or remove space around compare operator '<', '>', '==', etc
And:
align_left_shift { False, True }
Align lines that start with '<<' with previous '<<'. Default=true
You can try to play around with these parameters to be more closely to the solution although I would try something like:
sp_compare = Remove
align_left_shift = False
I am trying to write a GNU make call function (example below) which has multiple shell commands to execute, such that it can be called with different arguments.
shell_commands = $(shell echo $(1); ls -ltr $(2))
try:
$(call shell_commands,$(FILE1),$(FILE2))
1) Is above the correct way to write a call function with multiple commands? By using a semi-colon to separate them? To make it readable, I write my targets as shown below. Is there a similar way to write a call function?
shell_commands:
echo $(1)
ls -ltr $(2)
2) I get this error from make when I execute make -B try. It looks like it is trying to execute /home/user/file1. But why?
make: execvp: /home/user/file1: Permission denied
make: *** [try] Error 127
3) Is it possible to pass variable number of parameters to a call function? Like pass in just the second parameter and not the first one.
$(call shell_commands,,$(FILE2))
I tried google-ing, searching on SO, and looking on gnu.org but I did not get any solutions. Will appreciate any answers or pointers to any resources which document call function with multiple optional arguments and commands.
Question 1: No, this is not right. The shell make function should NEVER be used inside a recipe: the recipe is already running in the shell, so why would you run another shell? It's just confusing. Second, it's generally recommended to use && between multiple commands in a recipe, so that if the first command fails the entire command will immediately fail, rather than continuing on and perhaps succeeding. Of course, that is not always correct either, it depends on what you're trying to do.
Question 2: This happens because the shell make function is like backticks in the shell: it expands to the output printed by the shell command it runs. Your shell command that make runs is:
echo $(1); ls -ltr $(2)
(where, one assumes, $1 expands to /home/user/file1) which prints the string /home/user/file1. After the expansion, that string is substituted into the recipe and make tries to run that recipe, giving the error you see above.
You want this, most likely:
shell_commands = echo $(1) && ls -ltr $(2)
try:
$(call shell_commands,$(FILE1),$(FILE2))
Now the call expands to the actual text, not an invocation of make's shell function, and then that text is run as the recipe.
Question 3: Sure, just using empty parameters means that the variable $1 (in this case) expands to the empty string.
I'm trying to figure out how, if even possible, can you perform more than one command within a Haskell function? For example, if I had:
foo [[a]] = print (head a) --AND -- map (head of everything but the first value)
How would I go about doing something like that? Is there a function which I could use in place of --AND-- which would allow me to perform both commands on the list of lists?
You're probably looking for do notation, which combined with properly formed pattern matching would get your desired behavior:
foo :: Show a => [[a]] -> IO ()
foo xs = do
print $ head $ head xs -- first value in the 2D list
print $ map head $ tail xs -- Skip first sublist with tail
Control.Arrow module has many combinators for wiring inputs to functions in interesting ways. Fanouts, fan-ins, etc.
On my phone, but something like print (<<<) head (&&&) map ( f . tail) $ input . That is wrong functions and order of operations, but gives a hint of the flavor. Check the module docs.
i want to write little helper functions that stores and loads the octave session.
function restoreSession(filename)
history -r strcat('./states/',filename,'.history');
load("-binary", strcat('./states/',filename,'.data'))
endfunction
function saveSession(filename)
history -w strcat('./states/',filename,'.history');
save("-binary", strcat('./states/',filename,'.data'))
endfunction
The save/load command works well.
My Problem is that the history command seems not to evaulate the argument.
it prodces the following error:
syntax error
>>> history -r strcat('./states/',filename,'.history');
^
I already tried to use a temporary var for the path but in this case it only interprets the variable name as filename and complains about the missing file.
Does anybody has an idea how to solve this?
Use history with the function syntax instead of a command.
history ("-r", strcat ("./states/", filename, ".history"));
All commands are actually functions. The command syntax (when you don't use parentheses) is available to all functions, it just happens that for some it looks more natural. When you omit the parentheses, all the arguments are interpreted as strings, even variable names. If you want to do something fancier, call them as functions.
I'm just getting started on F#, and when playing around with operator overloading, I've run into something I don't quite understand. Now, I understand that you can't use, for example, +* as an overloaded prefix operator; it can only be an infix operator. Here's where I get confused, however:
let (+*) a = a + a * a;;
If I run this, fsi tells me that the function (+*) is an int->int. Great, I can dig that -- it's not an overloaded operator, just a normal function named (+*). So, if I do:
printf "%d" ((+*) 6)
I'll get 42, as I expect to. However, if I try:
printf "%d" (+*) 6
or
printf "%d" (+*)6
It won't compile. I can't put the exact error up right now as I don't have access to an F# compiler at this moment, but why is this? What's going on with the binding here?
It's interpreting this:
printf "%d" (+*) 6
Like this:
printf ("%d") (+*) (6)
In other words, passing three curried arguments to printf, the second of which is a reference to the function +*.