小编Pet*_*r V的帖子

像F#中的cond一样的clojure

我最近在F#中绕道而行,并遇到了一个叫做cond的宏.以下是用法示例:

(cond
 (= target (nth arr mid)) mid  
 (< target (nth arr mid)) (search left (- mid 1))
 (> target (nth arr mid)) (search (+ mid 1) right)
 (= left right) -1)
Run Code Online (Sandbox Code Playgroud)

这意味着以下伪代码:

if target == arr.[mid] then return mid
if target < arr.[mid] then return (call search(left, mid-1)) 
if target > arr.[mid] then return (call search(mid+1, right))
if left == right then return -1
Run Code Online (Sandbox Code Playgroud)

这只是二进制搜索的一个例子,以防你想知道左边和右边是什么,但不是很重要.

我试图在F#中找到类似的东西,但我不能,所以我决定尝试自己写.我最终得到了这样的东西:

type condition = bool * int

let cond (conds: condition seq) = 
    conds …
Run Code Online (Sandbox Code Playgroud)

f# clojure

7
推荐指数
2
解决办法
197
查看次数

标签 统计

clojure ×1

f# ×1