小编Kat*_*ina的帖子

如何使这段代码更紧凑和惯用?

Hullo all.

我是一名C#程序员,在我的空闲时间探索F#.我在2D中编写了以下用于图像卷积的小程序.

open System

let convolve y x = 
  y |> List.map (fun ye -> x |> List.map ((*) ye))
    |> List.mapi (fun i l -> [for q in 1..i -> 0] @ l @ [for q in 1..(l.Length - i - 1) -> 0])
    |> List.reduce (fun r c -> List.zip r c |> List.map (fun (a, b) -> a + b))   

let y = [2; 3; 1; 4]
let x = [4; 1; 2; 3]
printfn "%A" …
Run Code Online (Sandbox Code Playgroud)

f# functional-programming idiomatic

4
推荐指数
1
解决办法
389
查看次数

标签 统计

f# ×1

functional-programming ×1

idiomatic ×1