-
Notifications
You must be signed in to change notification settings - Fork 469
Description
Is your feature request related to a problem? Please describe.
I'm trying to understand Bend and write simple programs, after I've tried some from examples/ but still struggle to do so even with really simple ones. I've read docs/syntaxt.md but seems like it is incomplete/outdated.
Describe the solution you'd like
Please consider enabling Discussions in this repo for general questions like one I'm about to ask, so those could not pollute the issues.
Describe alternatives you've considered
I've tried to adapt example from GUIDE.md but lacking essential understanding how pieces should be arranged. I've tried to search for particular example I'm interested with inside docs: rg 'fold \w+ with' docs/ but there is no one.
Additional context
I'm trying to complete exercise "reverse list with fold" from GUIDE.md. I've great understanding of FP but mainly a LISP-family of languages, in particular - Scheme. In it, this is as simple as:
(import
(srfi srfi-241)) ; match
(define (reverse lst)
(let loop ([lst lst]
[acc '()])
(match lst
[(h . t)
(loop t (cons h acc))]
[()
acc])))Note that it doesn't actually uses fold (in FP sense), but essentially is the same.
Now that I'm trying to adapt "tree fold" from GUIDE.md to that simple problem (original "tree fold" quoted for clarity):
def enum(tree):
idx = 0
fold tree with idx: # <<< HERE 0
case Tree/Node:
return ![tree.left(idx * 2 + 0), tree.right(idx * 2 + 1)] # <<< HERE 1
case Tree/Leaf:
return !(idx, tree.value)
def main() -> Tree(u24):
tree = ![![!1, !2],![!3, !4]]
return enum(tree)What struggles me is that:
- it is not clear what
tree.left(idx * 2 + 0)means in 1
Is this a construction of tree? Is this a call? Why it uses () parens instead of {} if it is a construction?
In GUIDE.md, this is commented as:
- We pass new states down as
tree.xyz(new_idx)
What that .xyz actually mean? The type definition of a Tree doesn't have any "state" to be passed, isn't it?
type Tree:
Node { ~left, ~right }
Leaf { value }- it is not clear how to pass "new state" down to fold in this particular "reverse list" case, which is initialized to
List/Nilin 0
There is no description of fold ... with ... in docs/syntax.md, so I can only guess but none of my attempts are succeeded:
def reverse(list: List(T)) -> List(T):
res = List/Nil
fold list with res:
case List/Cons:
return List/Cons{list.head list.tail(res)} # or what???
case List/Nil:
return res
def main() -> List(u24):
return reverse([1,2,3])Trying to adapt "tree fold" here doesn't help, unfortunately.