简体   繁体   中英

How to implement a deep-tree-set algorithm using fold?

This is the pseudocode for a function that will modify a specific node on a tree given a path and a new value:

path_set val path tree = 
    | empty? path -> val
    | otherwise -> set (path_set val (tail path) (get tree (head path))) tree

For example,

path_set 50 [1 1] [[1 1] [1 1]] = [[1 1] [1 50]]

How can this algorithm be implemented using fold?

Well, as nobody answered, here is my solution:

(λ (λ (λ (foldr (λ (λ (set B (get 0 A) (get 1 A)))) C ((λ (λ (foldl (λ (λ (conc B (list ((list (A (if (eq (len B) 0) C (get (get 0 (last B)) (get 1 (last B))))))))))) (list) B))) B A)))))

In lambda calculus with bruijn indices encoded by alphabetically ascending letters and conc-lists.

It is really unoptimal, but works fine for my use case as it almost always reduced in compile time anyway (you almost always have the path defined when you want to call set ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM