2012-03-25 23 views
0

我想就如何應用此功能的暗示:整數列表,例如;在合併/合併整數使用char [哈斯克爾]列表

dti xs = (map intToDigit (take 6 (map digitToInt xs))++['/']++map intToDigit(drop 6 (map digitToInt xs)))

[1234567822,3245336792,...],所以我會得到類似[「123456/7822」,「324533/6792」,...]的輸出。

的一點是添加一個「/」後的整數列表,e.g的每個號碼的第6位; [1234567822,3245336792,...]。也許有比我更好的辦法。

+1

「splitAt」的解決方案可能比'take'和'drop'的組合更優雅。 – Joni 2012-03-25 21:25:59

+0

謝謝@JoniSalonen。 – baron 2012-03-26 11:07:48

回答

2

intToDigit期待一個單一的數字,所以它會產生一個錯誤的輸入像1234567822.

要轉換Int(或Integer)轉換成字符的列表,你可以使用show,然後拆分得到的字符串六位數後

format n = first ++ '/':second 
    where 
    s = show n 
    (first,second) = splitAt 6 s 

dti = map format 
+0

我知道像'first ++'/':second'這樣的代碼是慣用的,但我有時希望成語是'first ++'/'++ second'。 – 2012-03-26 07:06:07

+0

我可以理解。但是我不能讓自己把一個單一的角色包裝在一個列表中,只是爲了立即將它粘在另一個列表的前面。這是非常間接的。 – 2012-03-26 10:43:24

+0

非常感謝@DanielWagner。 – baron 2012-03-26 11:07:20