2015-01-11 40 views
1

我正在玩記錄和列表。請,我想知道如何使用一個變量兩次。當我指定的任何值到變量_list之後,我嘗試重寫這個變量,然後提高錯誤:在Erlang重寫變量

** exception error: no match of right hand side value

-module(hello). 
-author("anx00040"). 

-record(car, {evc, type, color}). 
-record(person, {name, phone, addresa, rc}). 
-record(driver, {rc, evc}). 

-record(list, {cars = [], persons = [], drivers = []}). 

%% API 
-export([helloIF/1, helloCase/1, helloResult/1, helloList/0, map/2, filter/2, helloListCaA/0, createCar/3, createPerson/4, createDriver/2, helloRecords/0, empty_list/0, any_data/0, del_Person/1, get_persons/1, do_it_hard/0, add_person/2]). 


createCar(P_evc, P_type, P_color) -> _car = #car{evc = P_evc, type = P_type, color = P_color}, _car 
    . 
createPerson(P_name, P_phone, P_addres, P_rc) -> _person= #person{name = P_name, phone = P_phone, addresa = P_addres, rc = P_rc}, _person 
    . 
createDriver(P_evc, P_rc) -> _driver = #driver{rc = P_rc, evc = P_evc}, _driver 
    . 

empty_list() -> 
    #list{}. 

any_data() -> 
    _car1 = hello:createCar("BL 4", "Skoda octavia", "White"), 
    _person1 = hello:createPerson("Eduard B.","+421 917 111 711","Kr, 81107 Bratislava1", "8811235"), 
    _driver1 = hello:createDriver(_car1#car.evc, _person1#person.rc), 

    _car2 = hello:createCar("BL 111 HK", "BMW M1", "Red"), 
    _person2 = hello:createPerson("Lenka M","+421 917 111 111","Krizn0, 81107 Bratislava1", "8811167695"), 
    _driver2 = hello:createDriver(_car2#car.evc, _person2#person.rc), 

    _car3 = hello:createCar("BL 123 AB", "Audi A1 S", "Black"), 
    _person3 = hello:createPerson("Stela Ba.","+421 918 111 711","Azna 20, 81107 Bratislava1", "8811167695"), 
    _driver3 = hello:createDriver(_car3#car.evc, _person3#person.rc), 

    _list = #list{ 
    cars = [_car1,_car2,_car3], 
    persons = [_person1, _person2, _person3], 
    drivers = [_driver1, _driver2, _driver3]}, 
    _list. 


add_person(List, Person) -> 
    List#list{persons = lists:append([Person], List#list.persons) }. 

get_persons(#list{persons = P}) -> P. 

do_it_hard()-> 
    empty_list(), 
    _list = add_person(any_data(), #person{name = "Test",phone = "+421Test", addresa = "Testova 20 81101 Testovo", rc =88113545}), 
    io:fwrite("\n"), 
    get_persons(add_person(_list, #person{name = "Test2",phone = "+421Test2", addresa = "Testova 20 81101 Testovo2", rc =991135455})) 
. 

但是,當我用它養錯誤變量_list兩次:

do_it_hard()-> 
    empty_list(), 
    _list = add_person(any_data(), #person{name = "Test",phone = "+421Test", addresa = "Testova 20 81101 Testovo", rc =88113545}), 
    _list =add_person(_list, #person{name = "Test2",phone = "+421Test2", addresa = "Testova 20 81101 Testovo2", rc =991135455}), 
    get_persons(_list) 
. 

回答

1

在REPL中,在重新使用變量名時可以方便地進行測試。在那裏,你可以做f(A).讓Erlang「忘記」A的當前任務。

1> Result = connect("goooogle.com"). 
{error, "server not found"} 
2> % oops! I misspelled the server name 
2> f(Result). 
ok 
3> Result = connect("google.com"). 
{ok, <<"contents of the page">>} 

請注意,這只是一個 REPL方便的特性。你不能在實際的代碼中做到這一點。

在實際代碼中,變量只能分配一次。在過程語言(C,使用Java,Python,等),典型的用例用於重新分配是循環:

for (int i = 0; i < max; i++) { 
    conn = connect(servers[i]); 
    reply = send_data(conn); 
    print(reply); 
} 

在上述中,變量iconn,和reply在的每個迭代被重新分配循環。

函數式語言使用遞歸來執行他們的循環:

send_all(Max, Servers) -> 
send_loop(1, Max, Servers). 

send_loop(Current, Max, _Servers) when Current =:= Max-> 
ok; 
send_loop(Current, Max, Servers) -> 
Conn = connect(lists:nth(Current, Servers)), 
Reply = send_data(Conn), 
print(Reply). 

這是不是很地道的二郎神;我試圖讓它反映上面的程序代碼。正如你所看到的,我得到了同樣的效果,但是我的作業在函數中的修復是固定不變的。

作爲一個方面說明,您正在使用大量以下劃線開頭的變量名。在Erlang中,這是暗示你不會使用這些變量的值。 (就像在上面的例子中,當我到達列表的末尾時,我不關心服務器列表)。在代碼中使用前導下劃線會關閉一些有用的編譯器警告,並會混淆其他任何開發人員誰看你的代碼。