Search code examples
erlanglfe

Not able to apply guard expression in query list comprehension


I am trying a query list comprehension:

> (set xs '(1 2 3 4 5 6 7 8))
> (lc ((<- x xs) (when (> x 5))) x) 

But I am getting the error exception error: undefined function when/1.

Is it possible to apply guard statements to lc?


Solution

  • According to the LFE User Guide, within a list comprehension qualifier, the guard must precede the list expression:

    (<- pat {{guard}} list-expr)

    This means your example should be written as follows:

    lfe> (set xs '(1 2 3 4 5 6 7 8))
    (1 2 3 4 5 6 7 8)
    lfe> (lc ((<- x (when (> x 5)) xs)) x)
    (6 7 8)
    

    You could alternatively treat the greater-than expression as a normal boolean expression qualifier:

    lfe> (lc ((<- x xs) (> x 5)) x)
    (6 7 8)