Info

Programmer by day, artist by night

Posts from the Uncategorized Category

The problem: From page 42, Exercise 3, Chapter 3, Programming Erlang by Joe Armstrong

Try representing a house using a tuple and a street using a list of houses. Make sure you can pack and unpack the data in the representations.

This is a very simple problem. Each house is a tuple, and the street is a list of tuples (houses). We create 4 houses that belongs to 4 famous fictional characters with varying reputation 🙂 Batman, Donald Duck, Sherlock Holmes and Spongebob Squarepants. We pack them in the street list (line 1) and then unpack them. First we get Batman’s house (line 2), then the resident (line 4).

Erlang/OTP 17 [erts-6.3]  [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V6.3  (abort with ^G)
1> Street = [{house, {resident, "Batman"},{address, "Wayne Manor, Gotham City"}}, {house, {resident, "Donald Duck"}, {address, "1313 Webfoot Walk, Duckburg, Calisota"}}, {house, {resident, "Sherlock Holmes"}, {address, "221B Baker Street"}}, {house, {resident, "Spongebob SquarePants"}, {address, "124 Conch Street, Bikini Bottom"}}].
[{house,{resident,"Batman"},
        {address,"Wayne Manor, Gotham City"}},
 {house,{resident,"Donald Duck"},
        {address,"1313 Webfoot Walk, Duckburg, Calisota"}},
 {house,{resident,"Sherlock Holmes"},
        {address,"221B Baker Street"}},
 {house,{resident,"Spongebob SquarePants"},
        {address,"124 Conch Street, Bikini Bottom"}}]
2> [HouseBatman,_,_,_] = Street.
[{house,{resident,"Batman"},
        {address,"Wayne Manor, Gotham City"}},
 {house,{resident,"Donald Duck"},
        {address,"1313 Webfoot Walk, Duckburg, Calisota"}},
 {house,{resident,"Sherlock Holmes"},
        {address,"221B Baker Street"}},
 {house,{resident,"Spongebob SquarePants"},
        {address,"124 Conch Street, Bikini Bottom"}}]
3> HouseBatman.
{house,{resident,"Batman"},
       {address,"Wayne Manor, Gotham City"}}
4> {_,HouseBatmanResident,_} = HouseBatman.
{house,{resident,"Batman"},
       {address,"Wayne Manor, Gotham City"}}
5> HouseBatmanResident.
{resident,"Batman"}
6>