2011-04-13 111 views
4

我正在測試一個表達式,其中列表理解的條件有兩個不等式。有沒有辦法在這裏做任務,而不是重複那個表達式? (下面的代碼無法正常工作,但我希望它會)Erlang列表理解

診斷(專長,PatientSymptoms) - >

{[CertainDisease|| 
    {CertainDisease,KnownSymptoms}<-Expertise, 
    C=length(PatientSymptoms)-length(PatientSymptoms--KnownSymptoms), 
    C>=2, 
    C<=5  
]}. 
+0

{NewDisease} =診斷([{D1,[S1,S2,S3]},{D2,[S1,S2,S3,S4] }],[S1,S2,S4 ])這是否回答你的問題? – titus 2011-04-13 03:00:23

回答

12

直接寫它沒有fun的方式將是使用一個begin ... end塊用布爾測試結束:

[ CertainDisease || {CertainDisease,KnownSymptoms} <- Expertise, 
        begin 
         C = length(PatientSymptoms) - length(PatientSymptoms -- KnownSymptoms), 
         C >= 2 andalso C <= 5 
        end ] 
6

定義過濾器功能;這樣一來,它被調用每個元素一次,免去您的計算C重複:

Filter = fun({CertainDisease, KnownSymptoms}) -> 
    C = length(PatientSymptoms) - length(PatientSymptoms--KnownSymptoms), 
    C >= 2 andalso C <= 5  
end 

並使用它在你的列表理解像這樣:

[CertainDisease || 
    {CertainDisease,KnownSymptoms} <- Expertise, 
    Filter({CertainDisease, KnownSymptoms})  
] 
+0

感謝特拉維斯,我現在就試試 – titus 2011-04-13 03:15:22

+1

只要過濾器「樂趣」與「PatientSymptoms」處於相同的動態範圍內,就應該工作。那是我一分鐘前就問的。 – 2011-04-13 03:17:51