[英]Haskell map function with where statement in F#
我正在嘗試將此haskell函數移植到F#
subs :: [a] -> [[a]]
subs [] = [[]]
subs (x:xs) = ys ++ map (x:) ys
where
ys = subs xs
例
潛艇[1,2,3]
返回:
[[],[3],[2],[2,3],[1],[1,3],[1,2],[1,2,3]]
返回列表的所有子序列,這些子序列由排除或包含每個元素的所有可能組合給出
....
我在“ where”語句中遇到問題,該語句遞歸生成其他列表“ ys”。
我也不確定我是否正確地將謂詞'(x :)'移植到'(fun i-> i)'。
這是我能弄清楚的F#語句。
let rec subs list =
match list with
| [] -> [[]]
| x::xs -> List.map (fun i -> i) xs
任何幫助或指示將不勝感激。
這是F#:
let rec subs list =
match list with
| [] -> [[]]
| x::xs ->
let ys = subs xs
ys @ List.map (fun t -> x::t) ys
printfn "%A" (subs [1;2;3])
Haskell的where
是非常就像let
移至底部。
在F#中, @
是列表串聯運算符, ::
是缺點。
F#中沒有運算符部分,因此我使用了lambda( fun
)。
讓我們看起來更像F#。 :)
let rec subs = function
| [] -> [[]]
| x::xs -> [ for ys in subs xs do
yield! [ys;x::ys] ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.