Arc Forumnew | comments | leaders | submit | phr's commentslogin
1 point by phr 5666 days ago | link | parent | on: A parser-combinator approach to parsing JSON

I don't understand the use of iflet here:

  arc> (def show-parse (parser str)
          (let p (coerce str 'cons)
            (iflet (p2 r) (parser p)
               (do (pr "match: ")
                   (write r)
                   (prn " remaining: " (coerce p2 'string)))
                   (prn "no match")))
           nil)

From the Arc Cross Reference http://practical-scheme.net/wiliki/arcxref?iflet the '(p2 r) should be a variable, but I'm not seeing it.

What am I missing?

-----

3 points by rntz 5666 days ago | link

Wherever you have a "variable" to be bound in arc, you can instead use an expression containing variables. So, for example, consider the following code:

    arc> (let x '(1 2) x)
    (1 2)
    arc> (let (x y) '(1 2) x)
    1
    arc> (let (x y) '(1 2) y)
    2
The first example is straightforward: we bind the variable x to the value (1 2). In the second and third examples, we bind the variables x and y to 1 and 2 respectively, by matching the value (1 2) against the binding expression (x y). In a fairly similar way, consider the following:

    arc> (let (head . tail) '(1 2 3) (list head tail))
    (1 (2 3))
In this case, we remember that (1 2 3) is really shorthand for (1 . (2 . (3 . nil))), and so we bind head to 1 and tail to (2 . (3 . nil)), or, abbreviated, to (2 3). Hence (list head tail) returns (1 (2 3)).

This simple but powerful concept is called "destructuring", because it takes a complex structure apart into its component values.

(I'm hoping that's the part you didn't understand... sorry if I missed the point.)

-----

1 point by phr 5666 days ago | link

Thanks. Makes sense now.

-----

2 points by phr 6145 days ago | link | parent | on: Forum Search?

That's it. Thanks!

-----