2013-10-14 67 views
-3

我學習Erlang和我試圖理解這個代碼,作爲一個例子。編程使用Erlang

-module(tutorial5). 
-export([format_temps/1]). 

format_temps(List_of_cities) -> 
    convert_list_to_c(List_of_cities). 

convert_list_to_c([{Name, {f, F}} | Rest]) -> 
    Converted_City = {Name, {c, (F -32)* 5/9}}, 
    [Converted_City | convert_list_to_c(Rest)]; 

convert_list_to_c([City | Rest]) -> 
    [City | convert_list_to_c(Rest)]; 

convert_list_to_c([]) -> 
    []. 

我不確定如何使用這些方法來獲得我所需要的。我知道這個最深的是,我應該能夠形成城市及其下的列表,然後能夠把自己的溫度華氏從轉換爲攝氏溫度,反之亦然。任何幫助,將不勝感激。

+0

你有一個具體的問題? – akonsu

回答

1

模塊tutorial5中唯一可調用的函數是format_temps/1(它需要一個參數)。這需要城市/溫度的列表,其中每個城市/ temp是形式{City,{f,Fahrenheit}}元組例如{berlin,{f,60}}。該函數返回臨時零件現在的城市/臨時表的列表{c,Celsius}。從它的返回外殼的示例調用將是:

> tutorial5:format_temps([{berlin,{f,59}},{london,{f,50}},{stockholm,{f,50}}]). 
[{berlin,{c,15.0}},{london,{c,10.0}},{stockholm,{c,10.0}}] 

幾點需要注意的是:

  • 當調用另一個模塊中的函數,你MUST總是包含模塊的名字
  • 首先是小寫字母的詞是原子,文字常量使用一個名稱,而那些開始大寫字母(從我的文字)是變量。外觀相似但非常不同。