[英]Haskell map function with where statement in F#
I am trying to port this haskell function to F# 我正在尝试将此haskell函数移植到F#
subs :: [a] -> [[a]]
subs [] = [[]]
subs (x:xs) = ys ++ map (x:) ys
where
ys = subs xs
example 例
subs [1,2,3] 潜艇[1,2,3]
returns: 返回:
[[],[3],[2],[2,3],[1],[1,3],[1,2],[1,2,3]] [[],[3],[2],[2,3],[1],[1,3],[1,2],[1,2,3]]
returns all sub sequences of a list, which are given by all possible combination of excluding or including each element 返回列表的所有子序列,这些子序列由排除或包含每个元素的所有可能组合给出
.... ....
I am having issues with the 'where' statement, which recursively generates the other list 'ys'. 我在“ where”语句中遇到问题,该语句递归生成其他列表“ ys”。
I am also not sure I port the predicate '(x:)' correctly to '(fun i -> i)'. 我也不确定我是否正确地将谓词'(x :)'移植到'(fun i-> i)'。
This is as much of the F# statement I can figure out. 这是我能弄清楚的F#语句。
let rec subs list =
match list with
| [] -> [[]]
| x::xs -> List.map (fun i -> i) xs
Any help or direction would be greatly appreciated. 任何帮助或指示将不胜感激。
Here's the F#: 这是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])
A Haskell where
is pretty much just like a let
moved to the bottom. Haskell的where
是非常就像let
移至底部。
In F#, @
is the list concatenation operator, and ::
is cons. 在F#中, @
是列表串联运算符, ::
是缺点。
There are no operator sections in F#, so I use a lambda ( fun
). F#中没有运算符部分,因此我使用了lambda( fun
)。
Let's get it to look more like F#. 让我们看起来更像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.