我想編寫一個函數來生成一個字符串。該字符串只包含1,0,s和S. 這些數字是二進制數字。每個數字通過s分隔。一個數字給出了字符串其餘部分的長度。大寫S是字符串的結尾。函數在Haskell中生成一個字符串
例子:
func :: Integral a => a -> String
func 1
"1S"
func 3
"110s11s1S"
func 4
"1010s110s11s1S"
我的問題是,我不知道,我怎麼能得到尾("s1S" -> tail
,11 -> head
)的長度,比獲得新的尾巴。
我的新代碼:
>toBinary :: Integral a => a -> String
>toBinary 0 = []
>toBinary x
> | mod x 2 == 0 = '0' : toBinary (div x 2)
> | mod x 2 == 1 = '1' : toBinary (div x 2)
>zubinaer :: Integral a => a -> String
>zubinaer x = reverse (toBinary x)
>
>distan :: Integral a => a -> String
>distan n = if n > 0 then hilfsfunktion (n-1) "1S" else []
>
> where
> hilfsfunktion :: Integral a => a -> String -> String
> hilfsfunktion 0 s = s
> hilfsfunktion n s = hilfsfunktion (n-1) (zubinaer(length s + 1) ++ "s" ++ s)
這裏我的老代碼:http://hpaste.org/54863
它是功課嗎? –
是的,是的。所以我想我可以得到一些提示。感謝您的幫助。 :) – Alexei
@Alexei該代碼is.pretty一個初學者沒關係。嘗試使用地圖!這真的可以節省你的工作。 – fuz