Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #71 Echo program implementation

Basic implementation of the Echo program: Print all arguments except the program name, separated by space, followed by newline.
The idiom demonstrates how to skip the first argument if necessary, concatenate arguments as strings, append newline and print it to stdout.

fun main(args: Array<String>) = args.forEach(::println)
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
begin
   for I in 1 .. Argument_Count loop
      Put (Argument (I) & (if I = Argument_Count then "" else " "));
   end loop;
   
   New_Line;
end Main;
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    while (*++argv) {
        printf("%s", *argv);
        if (argv[1]) printf(" ");
    }
    printf("\n");
    return EXIT_SUCCESS;
}
(println (clojure.string/join " " *command-line-args*))
#include <iostream>
int main(int argc, char * argv[])
{
	for(int i = 1; i < argc; ++i)
	{
		std::cout <<(1==i ? "" : " ") << argv[i];
	}
        std::cout <<std::endl;
}
using System;
public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine(string.Join(" ", args));
    }
}
import std.string, std.stdio;
void main(string[] args)
{
	args[1..$].join(" ").writeln;
}
import std.stdio : writeln;
import std.algorithm.iteration : joiner;
import std.range : dropOne;
void main(string[] args)
{
	writeln(args.dropOne.joiner(" "));
}
main(List<String> args) {
  print(args.join(' '));
}
Enum.join(args, " ")
-module(echo).
-export([main/1, main/0]).
main() ->
    main(init:get_plain_arguments()).

main(ARGV) ->
    io:format("~s~n", [lists:join(" ", ARGV)]).
program x
  implicit none
  character (len=:), allocatable :: a
  integer :: n, i, l
  n = command_argument_count()
  a = ''
  do i=1,n
     call get_command_argument(i, a, l)
     if (l > len(a)) then
        deallocate (a)
        allocate (character(len=l) :: a)
        call get_command_argument(i, a)
     end if
     write (unit=*,fmt='(A)', advance="no") a
     if (i < n) then
        write (unit=*,fmt='(" ")', advance="no")
     else
        write (unit=*,fmt='()')
     end if
  end do
end
import "fmt"
import "os"
import "strings"
func main() {
    fmt.Println(strings.Join(os.Args[1:], " "))
}
import System.Environment
putStrLn . unwords =<< System.Environment.getArgs
console.log(process.argv.slice(2).join(" "));
import static java.lang.String.join;
import static java.lang.System.out;
public class Echo {
    public static void main(final String... args) {
        out.println(join(" ", args));
    }
}
(format t "~{~A~^ ~}~%" *args*)
print( table.concat( arg, " " ) )
@import Foundation;
int main() {
  NSArray *all=NSProcessInfo.processInfo.arguments;
  NSArray *args=[all subarrayWithRange:NSMakeRange(1,all.count-1)];
  puts([args componentsJoinedByString:@" "].UTF8String);
  return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    while (*++argv) {
        printf("%s", *argv);
        if (argv[1]) printf(" ");
    }
    printf("\n");
    return EXIT_SUCCESS;
}
echo implode(' ', array_slice($argv, 1)), PHP_EOL;
PROGRAM Echo;

VAR
    I: Integer;
BEGIN
    for I := 1 to ParamCount - 1 do
        Write(ParamStr(I), ' ');
    if (ParamCount > 0) then
        Write(ParamStr(ParamCount));
    WriteLn();
END.
print "@ARGV\n"
main(Argv) :- echo(Argv).

echo([]) :- nl.
echo([Last]) :- write(Last), echo([]).
echo([H|T]) :- write(H), write(' '), echo(T).
import sys
print(' '.join(sys.argv[1:]))
printf("%s\n", ARGV.join(' '))
puts ARGV.join(' ')
use std::env;
println!("{}", env::args().skip(1).collect::<Vec<_>>().join(" "));
use itertools::Itertools;
println!("{}", std::env::args().skip(1).format(" "));
println(args.mkString(" "))
(display (string-join (list-tail (command-line) 1) " "))
(newline)
" Implementation: Visual Works "
| args arg sep |
args := CEnvironment commandLine readStream.
args next.    " skip executable name "
sep := [args peek isNil not ifTrue: [' '] ifFalse: ['']].
[ (arg := args next) isNil ] whileFalse: [
    Transcript show: arg; show: (sep value).
].
Transcript cr. " cr -> carriage return; ie. newline "

New implementation...
< >
christianhujer