我在尋求幫助,主要是因爲我對F#環境很新。我需要使用F#流來生成無限的阿姆斯壯數字流。任何人都可以幫助這個。我做了一些曼波巨無霸,但我不知道我要去哪裏。F#阿姆斯特朗數字流
type 'a stream = | Cons of 'a * (unit -> 'a stream)
let rec take n (Cons(x, xsf)) =
if n = 0 then []
else x :: take (n-1) (xsf());;
//to test if two integers are equal
let test x y =
match (x,y) with
| (x,y) when x < y -> false
| (x,y) when x > y -> false
| _ -> true
//to check for armstrong number
let check n =
let mutable m = n
let mutable r = 0
let mutable s = 0
while m <> 0 do
r <- m%10
s <- s+r*r*r
m <- m/10
if (test n s) then true else false
let rec armstrong n =
Cons (n, fun() -> if check (n+1) then armstrong (n+1) else armstrong (n+2))
let pos = armstrong 0
take 5 pos
那麼,你堅持?對我來說,這段代碼看起來是由兩個完全不同的人寫的('stream'的定義相當先進,但'test'函數有點奇怪)。 – 2014-10-28 09:31:55
它是否需要成爲你的''蒸汽'或你可以'seq'?請爲* Armstrong Number *添加一個不想去Google搜索的人的定義;) – Carsten 2014-10-28 09:38:37
正如Carsten指出的那樣,'seq'是: http://msdn.microsoft.com/en- gb/library/dd233209.aspx – Mau 2014-10-28 10:07:33