Modulinos: Difference between revisions
(Added Haskell) |
|||
Line 32: | Line 32: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
C++ programs |
C++ programs have scripted main by default. |
||
<lang cpp>#include <iostream> |
<lang cpp>#include <iostream> |
||
Line 58: | Line 58: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
Erlang programs |
Erlang programs have scripted main by default. |
||
<lang erlang>-module(scriptedmain). |
<lang erlang>-module(scriptedmain). |
||
Line 68: | Line 68: | ||
io:format("Number of Args: ~w~n", [length(Args)]), |
io:format("Number of Args: ~w~n", [length(Args)]), |
||
map (fun(Arg) -> io:format("Arg: ~s~n", [Arg]) end, Args).</lang> |
map (fun(Arg) -> io:format("Arg: ~s~n", [Arg]) end, Args).</lang> |
||
=={{header|Haskell}}== |
|||
Haskell programs also have scripted main by default. |
|||
<lang haskell>#!/usr/bin/env runhaskell |
|||
module ScriptedMain where |
|||
import System.Directory (getCurrentDirectory) |
|||
import System (getProgName, getArgs) |
|||
import Control.Monad (mapM_) |
|||
main :: IO () |
|||
main = do |
|||
directory <- getCurrentDirectory |
|||
program <- getProgName |
|||
args <- getArgs |
|||
putStrLn $ "Directory: " ++ directory |
|||
putStrLn $ "Program: " ++ program |
|||
putStrLn $ "Number of Args: " ++ (show . length) args |
|||
mapM_ (\x -> putStrLn $ "Arg: " ++ x) args</lang> |
Revision as of 23:31, 3 March 2011
You are encouraged to solve this task according to the task description, using any language you may know.
It is useful to be able to run a main() function only when a program is run directly. This is a central feature in programming scripts; the feature is called /scripted main/.
Examples from GitHub.
C
C programs have scripted main by default; as long as main() is not included in the header file, this program's (empty) API is accessible by other C code.
<lang c>#include "scriptedmain.h"
- include <stdio.h>
- include <unistd.h>
int main(int argc, char **argv) { char cwd[1024]; getcwd(cwd, sizeof(cwd));
printf("Directory: %s\n", cwd);
printf("Program: %s\n", argv[0]);
printf("Number of Args: %d\n", argc);
int i; for (i = 0; i < argc; i++) { printf("Arg: %s\n", argv[i]); }
return 0; }</lang>
C++
C++ programs have scripted main by default.
<lang cpp>#include <iostream>
- include <unistd.h>
using namespace std;
int main(int argc, char **argv) { char cwd[1024]; getcwd(cwd, sizeof(cwd));
cout << "Directory: " << cwd << endl;
cout << "Program: " << argv[0] << endl;
cout << "Number of Args: " << argc << endl;
int i; for (i = 0; i < argc; i++) { cout << "Arg: " << argv[i] << endl; }
return 0; }</lang>
Erlang
Erlang programs have scripted main by default.
<lang erlang>-module(scriptedmain). -import(lists, [map/2]).
main(Args) -> io:format("Directory: ~s~n", [filename:absname("")]), io:format("Program: ~s~n", [?FILE]), io:format("Number of Args: ~w~n", [length(Args)]), map (fun(Arg) -> io:format("Arg: ~s~n", [Arg]) end, Args).</lang>
Haskell
Haskell programs also have scripted main by default.
<lang haskell>#!/usr/bin/env runhaskell
module ScriptedMain where
import System.Directory (getCurrentDirectory) import System (getProgName, getArgs) import Control.Monad (mapM_)
main :: IO () main = do directory <- getCurrentDirectory program <- getProgName args <- getArgs
putStrLn $ "Directory: " ++ directory putStrLn $ "Program: " ++ program putStrLn $ "Number of Args: " ++ (show . length) args mapM_ (\x -> putStrLn $ "Arg: " ++ x) args</lang>