Sleep: Difference between revisions

From Rosetta Code
Content added Content deleted
(Vedit macro language added)
(formatting, corrections)
Line 23: Line 23:
end Sleep;</lang>
end Sleep;</lang>
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>
<lang AutoHotkey>TrayTip, sleeping, sleeping
TrayTip, sleeping, sleeping
sleep, 2000 ; 2 seconds
sleep, 2000 ; 2 seconds
TrayTip, awake, awake
TrayTip, awake, awake
Msgbox, awake
Msgbox, awake</lang>
</lang>


=={{header|BASIC}}==
=={{header|BASIC}}==
Line 91: Line 89:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>(defun test-sleep ()

<pre>
(defun test-sleep ()
(let ((seconds (read)))
(let ((seconds (read)))
(format t "Sleeping...~%")
(format t "Sleeping...~%")
Line 99: Line 95:
(format t "Awake!~%")))
(format t "Awake!~%")))


(test-sleep)
(test-sleep)</lang>
</pre>


=={{header|E}}==
=={{header|E}}==
Line 125: Line 120:


=={{header|Haskell}}==
=={{header|Haskell}}==
import Control.Concurrent
<lang haskell>import Control.Concurrent

main = do seconds <- readLn
main = do seconds <- readLn
putStrLn "Sleeping..."
putStrLn "Sleeping..."
threadDelay $ round $ seconds * 1000000
threadDelay $ round $ seconds * 1000000
putStrLn "Awake!"
putStrLn "Awake!"</lang>


=={{header|Java}}==
=={{header|Java}}==
Line 146: Line 141:


=={{header|Objective-C}}==
=={{header|Objective-C}}==
{{incorrect|Objective-C|Only the core of the task is implemented, not the task as a whole.}}<lang objc>[NSThread sleepForTimeInterval:numSeconds];

<lang objc>[NSThread sleepForTimeInterval:numSeconds]; // number of seconds can be fractional (it's a double)</lang>
// number of seconds can be fractional (it's a double)</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
Line 198: Line 193:


=={{header|Ruby}}==
=={{header|Ruby}}==

<lang ruby>seconds = gets.to_f
<lang ruby>seconds = gets.to_f
puts "Sleeping..."
puts "Sleeping..."
Line 206: Line 200:


=={{header|Standard ML}}==
=={{header|Standard ML}}==
<lang ocaml>(TextIO.print "input a number of seconds please: ";
{{incorrect|Standard ML|It doesn't fulfill the full requirements of the task.}}
<lang sml>let val seconds = valOf (Int.fromString (valOf (TextIO.inputLine TextIO.stdIn))) in
let val seconds = valOf (Int.fromString (valOf (TextIO.inputLine TextIO.stdIn))) in
TextIO.print "Sleeping...\n";
OS.Process.sleep (Time.fromReal seconds) (* it takes a Time.time data structure as arg,
OS.Process.sleep (Time.fromReal seconds); (* it takes a Time.time data structure as arg,
but in my implementation it seems to round down to the nearest second.
but in my implementation it seems to round down to the nearest second.
I dunno why; it doesn't say anything about this in the documentation *)
I dunno why; it doesn't say anything about this in the documentation *)
TextIO.print "Awake!\n"
end</lang>
end)</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl> puts -nonewline "Enter a number of milliseconds to sleep: "
<lang tcl>puts -nonewline "Enter a number of milliseconds to sleep: "
flush stdout
flush stdout
set millis [gets stdin]
set millis [gets stdin]
puts Sleeping...
puts Sleeping...
after $millis
after $millis
puts Awake!</lang>
puts Awake!</lang>


=={{header|Toka}}==
=={{header|Toka}}==
This makes use of the sleep() function from libc which suspends execution for a specified number of seconds.
This makes use of the sleep() function from libc which suspends execution for a specified number of seconds.
<pre>1 import sleep as sleep()
[ ." Sleeping...\n" sleep() drop ." Awake!\n" bye ] is sleep


45 sleep</pre>
1 import sleep as sleep()
[ ." Sleeping...\n" sleep() drop ." Awake!\n" bye ] is sleep
45 sleep

{{Omit From|Metafont}}

=={{header|Vedit macro language}}==
=={{header|Vedit macro language}}==
<lang vedit>#1 = Get_Num("Sleep time in 1/10 seconds: ")
<lang vedit>#1 = Get_Num("Sleep time in 1/10 seconds: ")
Line 236: Line 228:
Sleep(#1)
Sleep(#1)
Message("Awake!\n")</lang>
Message("Awake!\n")</lang>

{{Omit From|Metafont}}

Revision as of 12:50, 16 July 2009

Task
Sleep
You are encouraged to solve this task according to the task description, using any language you may know.

Write a program that does the following in this order:

  • Input an amount of time to sleep in whatever units are most natural for your language (milliseconds, seconds, ticks, etc.). This unit should be noted in comments or in a description.
  • Print "Sleeping..."
  • Sleep the main thread for the given amount of time.
  • Print "Awake!"
  • End.

Ada

The Ada delay statement takes an argument of type Duration, which is a real number counting the number of seconds to delay. Thus, 2.0 will delay 2.0 seconds, while 0.001 will delay 0.001 seconds.

<lang ada>with Ada.Text_Io; use Ada.Text_Io; with Ada.Float_Text_Io; use Ada.Float_Text_Io;

procedure Sleep is

  In_Val : Float;

begin

  Get(In_Val);
  Put_Line("Sleeping...");
  delay Duration(In_Val);
  Put_Line("Awake!");

end Sleep;</lang>

AutoHotkey

<lang AutoHotkey>TrayTip, sleeping, sleeping sleep, 2000 ; 2 seconds TrayTip, awake, awake Msgbox, awake</lang>

BASIC

Works with: QuickBasic version 4.5

<lang qbasic>INPUT sec 'the SLEEP command takes seconds PRINT "Sleeping..." SLEEP sec PRINT "Awake!"</lang> "SLEEP" with no argument will sleep until a button is pressed on the keyboard (including modifier keys such as shift or control). Also, pressing a key while SLEEP is waiting for a specific amount of time (as above) will end the SLEEP.

C

Works with: POSIX

The function sleep needs seconds, which are read from the standard input.

<lang c>#include <stdio.h>

  1. include <unistd.h>

int main() {

 unsigned int seconds;
 scanf("%u", &seconds);
 printf("Sleeping...\n");
 sleep(seconds);
 printf("Awake!\n");
 return 0;

}</lang>

C++

<lang cpp>#include <unistd.h>

  1. include <iostream>

using namespace std;

int main(int argc, char* argv[]) {

   useconds_t microseconds;
   cin >> microseconds;
   cout << "Sleeping..." << endl;
   usleep(microseconds);
   cout << "Awake!" << endl;
   return 0;

}</lang>

C#

<lang csharp>using System; using System.Threading;

class Program {

   static void Main(string[] args)
   {
       int sleep = Convert.ToInt32(Console.ReadLine());
       Console.WriteLine("Sleeping...");
       Thread.Sleep(sleep); //milliseconds
       Console.WriteLine("Awake!");
   }

}</lang>

Common Lisp

<lang lisp>(defun test-sleep ()

 (let ((seconds (read)))
   (format t "Sleeping...~%")
   (sleep seconds)
   (format t "Awake!~%")))

(test-sleep)</lang>

E

You can't do that.

No, really. E's approach to timing, concurrency, and IO is non-blocking; if you want to wait for something, you say what you want to do when it happens — i.e. callbacks. There are no threads of control which can be stopped — except automatically when they just have nothing to do.

So, the closest thing possible to the task description is to wait for the specified time to pass, then do whatever the next thing is.

def sleep(milliseconds :int, nextThing) {
    stdout.println("Sleeping...")
    timer.whenPast(timer.now() + milliseconds, fn {
        stdout.println("Awake!")
        nextThing()
    })
}

Forth

: sleep ( ms -- )
  ." Sleeping..."
  ms
  ." awake." cr ;

Haskell

<lang haskell>import Control.Concurrent

main = do seconds <- readLn

         putStrLn "Sleeping..."
         threadDelay $ round $ seconds * 1000000
         putStrLn "Awake!"</lang>

Java

<lang java>import java.util.Scanner;

public class Sleep{ public static void main(String[] args) throws InterruptedException{ Scanner input = new Scanner(System.in); int ms = input.nextInt(); //Java's sleep method accepts milliseconds System.out.println("Sleeping..."); Thread.sleep(ms); System.out.println("Awake!"); } }</lang>

Objective-C

This example is incorrect. Please fix the code and remove this message.

Details: Only the core of the task is implemented, not the task as a whole.

<lang objc>[NSThread sleepForTimeInterval:numSeconds];

// number of seconds can be fractional (it's a double)</lang>

OCaml

<lang ocaml>#load "unix.cma";; let seconds = read_int ();; print_endline "Sleeping...";; Unix.sleep seconds;; (* number is integer in seconds *) print_endline "Awake!";;</lang>

or <lang ocaml>#load "unix.cma";;

  1. directory "+threads";;
  2. load "threads.cma";;

let seconds = read_float ();; print_endline "Sleeping...";; Thread.delay seconds;; (* number is in seconds ... but accepts fractions *) print_endline "Awake!";;</lang>

Perl

seconds: <lang perl>$seconds = <>; print "Sleeping...\n"; sleep $seconds; # number is in seconds print "Awake!\n";</lang>

microseconds and nanoseconds using the Time::HiRes module: <lang perl>use Time::HiRes qw( usleep nanosleep );

$microseconds = <>; print "Sleeping...\n"; usleep $microseconds; print "Awake!\n";

$nanoseconds = <>; print "Sleeping...\n"; nanosleep $nanoseconds; print "Awake!\n";</lang>

Python

<lang python>import time

seconds = float(raw_input()) print "Sleeping..." time.sleep(seconds) # number is in seconds ... but accepts fractions

  1. Minimum resolution is system dependent.

print "Awake!"</lang>

Ruby

<lang ruby>seconds = gets.to_f puts "Sleeping..." sleep(seconds) # number is in seconds ... but accepts fractions

  1. Minimum resolution is system dependent.

puts "Awake!"</lang>

Standard ML

<lang ocaml>(TextIO.print "input a number of seconds please: "; let val seconds = valOf (Int.fromString (valOf (TextIO.inputLine TextIO.stdIn))) in

 TextIO.print "Sleeping...\n";
 OS.Process.sleep (Time.fromReal seconds);  (* it takes a Time.time data structure as arg,
                                              but in my implementation it seems to round down to the nearest second.
                                              I dunno why; it doesn't say anything about this in the documentation *)
 TextIO.print "Awake!\n"

end)</lang>

Tcl

<lang tcl>puts -nonewline "Enter a number of milliseconds to sleep: " flush stdout set millis [gets stdin] puts Sleeping... after $millis puts Awake!</lang>

Toka

This makes use of the sleep() function from libc which suspends execution for a specified number of seconds.

1 import sleep as sleep()
[ ." Sleeping...\n" sleep() drop ." Awake!\n" bye ] is sleep

45 sleep

Vedit macro language

<lang vedit>#1 = Get_Num("Sleep time in 1/10 seconds: ") Message("Sleeping...\n") Sleep(#1) Message("Awake!\n")</lang>