Search code examples
schemelet

As standard, does Scheme have any syntactic sugar for trivial calls to let?


I find that let has too many brackets. For example, when writing the following block of code, it was very easy to misplace lets many closing brakcets.

(define (adder n)
    (let ((a 1))
        (+ a n)))

All of these brackets are clearly necessary for huge let blocks, but they feel redundant for smaller calls. Does Scheme have any syntactic sugar for trivial calls to let? For example, what about cases where I only want to locally bind one variable? I had considered define, but apparent it's dangerously dependent on your choice of implementation. Is there any built-in solution to this that doesn't share that flaw? I'd prefer to not have to write a macro. Essentially, I'm looking for the procedure that is to let what if is to cond.


Solution

  • The only other way to do it is with a lambda I guess:

    ((lambda (a) (+ a n) 1)
    

    But:

    I find that let has too many brackets.

    You may need to switch to another language (it may sound harsh, but I mean it in a friendly way). If the number of parentheses is too high you might better enjoy languages that have less parentheses overall, because in Scheme no amount of macros will remove the fact that parentheses are frequent characters to see.

    [...] it was very easy to misplace lets many closing brakcets.

    It can be the case if you are writing your code in an editor that does not highlight matching parentheses and does not indent nested forms. Because in editors that support Lisp and Scheme (e.g. Emacs), misplacing a parentheses is usually not a risk (or at least, it becomes quickly apparent that there is a mistake if the (automatic) indentation goes too much on left or on right).