[erlang-questions] Question on pattern matching from Joe's book

Vance Shipley vances@REDACTED
Sat Jul 28 15:22:27 CEST 2007


On Sat, Jul 28, 2007 at 01:45:59AM +0530, Prakash Swaminathan wrote:
}  On page 43 of the pdf book, under section 2.12 "Pattern Matching
}  again", the second-to-last example: [H T] "cat" Succeeds H ->99, T
}  ->"at".
}  
}  Why is the value assigned to T-> "at" and not "97116"?

The list [97,116] is quite different from the string "97116".
Strings are really just lists, it's only a shorthand and the
shell will attempt to help you by displaying lists which are
all character values as strings.  

Characters are really just numbers.  You may use the notation
$char as a shorthand to deal with characters as numbers.

So the following three representations are equivalent:

1> "cat" = [$c,$a,$t] = [99,97,116].
"cat"

... as are these three:

2> "97116" = [$9,$7,$1,$1,$6] = [57,55,49,49,54].
"97116"

So when you evaulate the example:

3> [H|T] = "cat".
"cat"

... H is assigned the first character in the list:

4> H = $c = 99.
99

... while T is assigned the tail of the list:

5> T = [$a,$t] = [97,116].
"at"

	
	-Vance



More information about the erlang-questions mailing list