[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Guile Assembler

From: Panicz Maciej Godek
Subject: Re: Guile Assembler
Date: Tue, 23 Jun 2015 23:33:06 +0200

> > Hm. Where's the difference to Guile's define? And why do you have double
> > parentheses in your example?
> >
> > Still a bit lost.
> you read the pasted code in the repo? ;-)

Not yet, I must admit. But nevermind, got it. It looks like a definition
for a parametric func or for a half-curried func, depending on how you
squint at it ;-)

That explains the second set of parens.

sorry to answer that late, but I erroneously sent the previous answer only to Nala.

I have another project (in another repo) which provides the (extra common) module, which redefines "define" and "lambda" forms (among others):

(The module is rather huge -- it is a bag that I carry around)

The modification of "define" originates, I believe, from the book "Structure and Interpretation of Classical Mechanics" by Gerald Sussman and Jack Wisdom [1], but I stole that idea directly from Guile's (ice-9 curried-definitions) module [2]. The idea is that

(define ((f x) y)
  (list x y))

is equivalent to

(define f
  (lambda (x)
    (lambda (y)
       (list x y))))

This behaviour is consequent with the idea that

(define (g x)
  (* x x))

should be equivalent to

(define g
  (lambda (x)
    (* x x)))

The derivative variants are easier to read, because they make it apparent, that e.g. (g 5) can be substituted with (* 5 5), and -- similarly -- ((f 2) 3) can be substituted with (list 2 3).

The other difficulty is that the original function header looked like this:

(define ((number/base base) (l ...))

so that the form of the second argument is "(l ...)". This is because the (extra common) module allows to destructure the arguments using the (ice-9 match) pattern matcher, so that the above header is actually equivalent to

(define number/base
  (lambda (base)
    (lambda x
      (match x
        ((l ...)

In practice that form of argument doesn't do much. It just tells the reader that the argument is a proper list (because only a proper list matches such pattern)

Similarly, I could write code like

(map (lambda ((a . b)) (+ a b)) '((1 . 2)(3 . 4)(5 . 6)))

I also use that trick with let and let* forms:

(let (((x y) '(1 2)))
  (+ x y))

In addition, the module allows me to use the "let" and "let*" forms with multiple values:

(let ((a (b c) (values 1 '(2 3))))
  (+ a b c))

Best regards


reply via email to

[Prev in Thread] Current Thread [Next in Thread]