Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(define) is not allowed inside a (lambda) form #17

Closed
euhmeuh opened this issue Nov 13, 2018 · 1 comment
Closed

(define) is not allowed inside a (lambda) form #17

euhmeuh opened this issue Nov 13, 2018 · 1 comment

Comments

@euhmeuh
Copy link

euhmeuh commented Nov 13, 2018

This works:

(urlang
  (urmodule js
    (define (hello x) (+ "Hello " x))
    (hello "world")))
"use strict";
function hello(x){return ("Hello "+x);};
(hello("world"));

This does not and seems to trigger a Racket parsing error:

(urlang
  (urmodule js
    (lambda ()
      (define (hello x) (+ "Hello " x))
      (hello "world"))))
; readline-input:29:7: parse-expr: expected an expression, got
;   #<syntax:readline-input:29:7 define>
;   in: define
; [,bt for context]

This does not either and is what I'd like to do:

(urlang
  (urmodule js
    (var [y (lambda ()
              (define (hello x) (+ "Hello " x))
              (hello "world"))])))
; readline-input:42.4: var: expected Fixnum; expected Flonum; expected String;
;   expected Boolean; expected Application; expected Sequence; expected
;   Ternary; expected Assignment; expected Let; expected Lambda; expected
;   ArrayReference; expected Array; expected New; expected Object; expected
;   Dot; expected Id; or expected one of these identifiers: `while', `var',
;   `block', `do-while', `sif', `break', `continue', `return', `label',
;   `begin', `if', `:=', `let', `lambda', `λ', `ref', `array', `new', `object',
;   or `dot'
;   at: define
;   in: (var (y (lambda () (define (hello x) (+ "Hello " x)) (hello "world"))))
;   parsing context:
;    while parsing different things...
;    while parsing Body
;     term: ((define (hello x) (+ "Hello " x)) (hello "worl...
;     location: readline-input:42.12
;    while parsing Lambda
;     term: (lambda () (define (hello x) (+ "Hello " x)) (h...
;     location: readline-input:42.12
;    while parsing Expr
;     term: (lambda () (define (hello x) (+ "Hello " x)) (h...
;     location: readline-input:42.12
;    while parsing VarBinding
;     term: (y (lambda () (define (hello x) (+ "Hello " x))...
;     location: readline-input:42.9
; [,bt for context]

I guess (define) is missing from the allowed syntax in (lambda) or in (var).

@soegaard
Copy link
Owner

As discussed in the #urlang channel at Slack.

Local definitions used define are not allowed in the body of a lambda-expression.
The natural translation to JavaScript, would be to use function or var, but the semantics
of function differ from the semantics of define in Racket. It would be nice to
support a define-line form, but right now one needs to use var instead.

The error message was particular unhelpful, so I have committed an improvement
to parse-lambda with a better message.

See 86403f0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants