以下代码查找| c[0] | c[1] | c[2] | c[3] | c[4] | c[5] |
x a \0 \0 \0 c c c \0 \0 b b \0 \0 \0 j \0 \0 \0 \0 a \0 \0 \0 \0 d \0 \0 \0 \0
给出值列表。 maxLen
是一个可变值。如何删除它以进行更多功能练习?
maxLen
答案 0 :(得分:2)
如果您更改getNewPostAndSetMaxLength
以获取当前最大长度并返回一对(pos, maxLength)
,则可以使用List.fold
:
let getNewPosAndSetMaxLength maxLen pos len = // For each new position, check if the result of `getLenth pos` is larger than previous ones.
let direction = if len < 1 then -1 else 1
let newPos =
match [1..abs(len)] |> List.tryFind (fun i -> reject (pos + i * direction)) with
| Some p -> p
| None -> pos + len
let newLen = getLength newPos
let maxLen' = if newLen > maxLen then newLen else maxLen
(newPos, maxLen')
[<EntryPoint>]
let main argv =
let (lastPos, maxLen) = [3; -2; 6; 7] |> List.fold (fun (pos, maxLen) x ->
getNewPosAndSetMaxLength maxLen pos x) (0, 0)
printfn "%d" maxLen
0 // return an integer exit code