Undefined values: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Haskell.)
m (→‎{{header|Haskell}}: "1/0" -> "1 `div` 0")
Line 8: Line 8:
This isn't quite as dangerous as it sounds because of Haskell's laziness. For example, this program:
This isn't quite as dangerous as it sounds because of Haskell's laziness. For example, this program:


<lang haskell>main = print $ length [undefined, undefined, 1/0]</lang>
<lang haskell>main = print $ length [undefined, undefined, 1 `div` 0]</lang>


prints <code>3</code>, since <code>length</code> doesn't need to evaluate any of the elements of its input.
prints <code>3</code>, since <code>length</code> doesn't need to evaluate any of the elements of its input.

Revision as of 00:15, 2 December 2009

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

For languages which have an explicit notion of an undefined value, identify and exercise those language's mechanisms for identifying and manipulating a variable's value's status as being undefined

Haskell

The nullary function undefined returns the undefined value. But Haskell has no way whatsoever to identify or manipulate an undefined value at runtime. In fact, trying to evaluate anything that's undefined terminates the program immediately.

<lang haskell>main = print $ 2 + undefined -- An error.</lang>

This isn't quite as dangerous as it sounds because of Haskell's laziness. For example, this program:

<lang haskell>main = print $ length [undefined, undefined, 1 `div` 0]</lang>

prints 3, since length doesn't need to evaluate any of the elements of its input.

In practice, one uses undefined less often than error, which behaves exactly the same except that it lets you choose the error message. So if you say

<lang haskell>resurrect 0 = error "I'm out of orange smoke!"</lang>

then if you make the mistake of writing your program such that it at some point requires the value of resurrect 0, you'll get the error message "I'm out of orange smoke!".

Perl

<lang perl>#!/usr/bin/perl -w use strict;

  1. Declare the variable

our $var;

  1. Check to see whether it is defined

print "var contains an undefined value at first check\n" unless defined $var;

  1. Give it a value

$var = "Chocolate";

  1. Check to see whether it is defined after we gave it the
  2. value "Chocolate"

print "var contains an undefined value at second check\n" unless defined $var;

  1. Give the variable an undefined value.

$var = undef;

  1. or, equivalently:

undef $var;

  1. Check to see whether it is defined after we've explicitly
  2. given it an undefined value.

print "var contains an undefined value at third check\n" unless defined $var;

  1. Give the variable a value of 42

$var = 42;

  1. Check to see whether the it is defined after we've given it
  2. the value 42.

print "var contains an undefined value at fourth check\n" unless defined $var;

  1. Because most of the output is conditional, this serves as
  2. a clear indicator that the program has run to completion.

print "Done\n";</lang>

Results in:

var contains an undefined value at first check
var contains an undefined value at third check
Done

PHP

<lang php><?php // Check to see whether it is defined if (!isset($var))

   echo "var is undefined at first check\n";

// Give it a value $var = "Chocolate";

// Check to see whether it is defined after we gave it the // value "Chocolate" if (!isset($var))

   echo "var is undefined at second check\n";

// Give the variable an undefined value. unset($var);

// Check to see whether it is defined after we've explicitly // given it an undefined value. if (!isset($var))

   echo "var is undefined at third check\n";

// Give the variable a value of 42 $var = 42;

// Check to see whether the it is defined after we've given it // the value 42. if (!isset($var))

   echo "var is undefined at fourth check\n";

// Because most of the output is conditional, this serves as // a clear indicator that the program has run to completion. echo "Done\n"; ?></lang>

Results in:

var is undefined at first check
var is undefined at third check
Done

Python

<lang python># Check to see whether it is defined try: var except NameError: print "var is undefined at first check"

  1. Give it a value

var = "Chocolate"

  1. Check to see whether it is defined after we gave it the
  2. value "Chocolate"

try: var except NameError: print "var is undefined at second check"

  1. Give the variable an undefined value.

del var

  1. Check to see whether it is defined after we've explicitly
  2. given it an undefined value.

try: var except NameError: print "var is undefined at third check"

  1. Give the variable a value of 42

var = 42

  1. Check to see whether the it is defined after we've given it
  2. the value 42.

try: var except NameError: print "var is undefined at fourth check"

  1. Because most of the output is conditional, this serves as
  2. a clear indicator that the program has run to completion.

print "Done"</lang>

Results in:

var is undefined at first check
var is undefined at third check
Done

Ruby

<lang ruby># Check to see whether it is defined puts "var is undefined at first check" unless defined? var

  1. Give it a value

var = "Chocolate"

  1. Check to see whether it is defined after we gave it the
  2. value "Chocolate"

puts "var is undefined at second check" unless defined? var

  1. I don't know any way of undefining a variable in Ruby
  1. Because most of the output is conditional, this serves as
  2. a clear indicator that the program has run to completion.

puts "Done"</lang>

Results in:

var is undefined at first check
Done

Tcl

Tcl does not have undefined values, but variables may be undefined by being not set. <lang tcl># Variables are undefined by default and do not need explicit declaration

  1. Check to see whether it is defined

if {![info exists var]} {puts "var is undefind at first check"}

  1. Give it a value

set var "Screwy Squirrel"

  1. Check to see whether it is defined

if {![info exists var]} {puts "var is undefind at second check"}

  1. Remove its value

unset var

  1. Check to see whether it is defined

if {![info exists var]} {puts "var is undefind at third check"}

  1. Give it a value again

set var 12345

  1. Check to see whether it is defined

if {![info exists var]} {puts "var is undefind at fourth check"}

puts "Done"</lang> Yields this output:

var is undefind at first check
var is undefind at third check
Done