lambda functions

13 views
Skip to first unread message

William Haynes

unread,
Jun 18, 2025, 9:40:07 AMJun 18
to Numbas Users
Hi Christian,

I don't know if this a bug or not, but lambda functions don't like leading minus signs.

x -> -2x          Not enough arguments for operation lambda
x -> (-2)*x       Can't find variable: names_tree
x -> x(-2)        OK
x -> 0-2x         OK

Will

Christian Lawson-Perfect

unread,
Jun 19, 2025, 9:31:29 AMJun 19
to numbas...@googlegroups.com
Thanks for reporting this. I think I just hadn't thought of those cases when I added lambda functions to the parser. I've now fixed it and added some unit tests.

--
You received this message because you are subscribed to the Google Groups "Numbas Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to numbas-users...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/numbas-users/01137c42-270d-44c8-9835-09d98eb0dbd9n%40googlegroups.com.

William Haynes

unread,
Jun 19, 2025, 11:05:17 AMJun 19
to 'guillaume theo' via Numbas Users
HI Christian,

Thank you for fixing the lambda function.

Here is another thing that looks like a bug to me.  The "isa" function is not properly handing elements of vectors or lists.

let(V,vector(1.1,2.2), 
  L,[1.1,2.2], 
  [type(V), 
    V isa "vector", 
    type(V[0]), 
    V[0] isa "number", 
    type(L[0]), 
    L[0] isa "number"
])

returns:

[ "vector", true, "number", false, "number", false ]

which I think should be:

[ "vector", true, "number", true, "number", true ]


Will

William Haynes Engineering Department / Professor Massachusetts Maritime Academy 100 Academy Drive Buzzards Bay, MA 02532 wha...@maritime.edu

Christian Lawson-Perfect

unread,
Jun 20, 2025, 3:32:13 AMJun 20
to numbas...@googlegroups.com
Thanks again.

It looks from the code like `isa` never evaluates the thing on the left, so `v[0]` isn't a number token, it's an application of the list index operation. That doesn't feel particularly useful to me, and it certainly isn't what you'd expect.
I've changed it to evaluate the expression, so your cases now work as you expected.

William Haynes

unread,
Jun 25, 2025, 10:18:05 AMJun 25
to Numbas Users
Hi Christian,

I've run into another issue.   Context: I was making a function to calculate the area of a trapezoid..

(xa, xb, ya, yb) ->   (xb-xa)(ya+yb)/2.      Error evaluating variable area: Variable ya is not defined.
(xa, xb, ya, yb) -> 1*(xb-xa)(ya+yb)/2.      OK

Will

Christian Lawson-Perfect

unread,
Jun 27, 2025, 6:11:04 AMJun 27
to numbas...@googlegroups.com
I think this is a bracketing issue: it interprets `(a,b) -> (a)(b)` as the application of the function `(a,b) -> (a)` to the argument `b`. If you put brackets around the whole body of the function, i.e. `(a,b) -> ((a)(b))`, then it's interpreted correctly.

I've spent a couple of hours fixing the parser so that it deals better with implicit multiplication in and after anonymous functions.

Thanks again for reporting this!

William Haynes

unread,
Jun 27, 2025, 12:59:37 PMJun 27
to Numbas Users
That sounds like a tricky one. Thanks for all your work on this project.
Reply all
Reply to author
Forward
0 new messages