libera/#lisp - IRC Chatlog
Search
7:28:59
rendar
can we say that apply internally checks the number of arguments that the functions accepts, and if its 2, and the list of params len > 2, it will act like a reduce
7:36:55
wasamasa
you can translate a function accepting several arguments to a function accepting one argument less
7:42:12
wasamasa
if you want to expose apply to the lisp interpreter, it's slightly bit more complicated
7:51:40
rendar
wasamasa, the problem with `return func(*func_args)` is it works when i call (+ 1 2), but it doesn't when i call (+ 1 2 3)
7:52:23
wasamasa
in scheme (+) returns 0, (+ 1) returns 1, (+ 1 2) returns 3 and (+ 1 2 3) returns 6
7:53:27
wasamasa
you need to implement + in terms of functools.reduce, not apply in terms of functools.reduce
7:54:22
wasamasa
no, you need to look at every individual function you need to implement and decide how
7:54:57
rendar
the point is that in lisp every 2-ary functions like +, -, / and so on, seem to take N args
7:55:27
rendar
a kind of minimal lisp just to insert commands in a system, minimal ability to create functions and macros
7:57:51
wasamasa
you still need to decide how basic arithmetic should behave and how a useful standard library looks like
9:17:31
rendar
also, there must be some internal mechanism that will map (- 1) to an unary not function, and (- 1 1) to 2-ary minus function..
9:19:46
wasamasa
but you might as well just have your minus function check for the number of arguments itself
9:27:37
pjb
(defun - (arg &rest args) (if (null args) (* -1 arg) (+ arg (* -1 (apply (function +) args)))))
9:32:16
sham1
(define - (case-lambda ((x) (negate x)) ((x . ys) (actually-subtract x (reduce + 0 ys)))))
11:49:53
jcowan
As for Scheme not being CL-like, it is far more CL-like than most other Lisps: Interlisp, Picolisp, Clojure. All of them are descendants of Lisp 1.5.
12:11:09
mdhughes_
(- 1) is just equivalent to (- 0 1) of course; it could be implemented as fold from 0, same as +
12:36:00
Alfr
mdhughes_, so you still have to decide based upon how many arguments you get, nothing gained. And then whether you say 0-x or additive inverse of x, doesn't really. I like the second one more, but that may be a personal preference.
12:37:34
aeth
Alfr: anything that + and - are defined to work on, since this is #lisp and not #commonlisp or #scheme :-p
12:37:51
aeth
but we are working on something purely symbolic and much more abstract than anything concrete
12:38:17
aeth
perhaps x and y are true real numbers (or at least, the ones that are computable) rather than floats/rationals/integers
12:40:59
aeth
(which is afaik equivalent to (- (+) A) as long as (+) produces a zero matrix the same size as A)
12:41:52
Alfr
aeth, so, you have +, e, +-inverse? And since you certainly want (+ a b c ..) to be unambiguous, you want + to associative. That group gives you a group, I think.
12:45:12
aeth
yes, we want Abstract Lisp's + to work on groups, while / for more than one argument should be restricted to fields
12:45:18
Alfr
aeth, I also think, that you want to be able to write (+ a b (+ c ..) d ..), so + is also closed. (I always forget this one.)
12:57:51
aeth
I suppose (+ ...) and (+) should only be used when - is defined, and otherwise prefer (* ...) and (*) even if it's not a field. Strange cases include matrices, which would have +, *, and even (/ A) defined, but afaik not (/ A B)
13:00:05
Alfr
Assuming you have (/ A) then you also have (/ B A) by (* (/ A) B) (or equivalently (* B (/ A))).
13:00:34
jcowan
mdhughes_: Load SRFI 1 and mess with fold and fold-right; you will quickly find that neither fold nor fold-right implements variadic -.
13:02:51
mdhughes_
aeth: You would not actually use * to implement negation, that's much slower than (- 0 x), even for floats.
13:14:53
mdhughes_
(define (minus . rest) (fold mm (if (null? (cdr rest)) 0 (car rest)) (if (null? (cdr rest)) rest (cdr rest))))
13:16:23
mdhughes_
Makes sense if you're using cons or other things that take the accumulator right, but it's nonsensical in math.
13:23:49
jcowan
(btw, neither left nor right fold, neither accumulator-first nor accumulator-second, will allow you to correctly implement - as a fold)
13:26:05
jcowan
As a result of historical factors, vector-like SRFIs use the accumulator-first style and all other sequence SRFIs use the accumulator-second style.
13:26:48
jcowan
A good approach for Committee C to clean this up, IMAO, is to use the names fold and fold-right for accumulator-second (SRFI 1) style and the names lfold and rfold for accumulator-first style.
13:30:51
jcowan
Nope. Try the four cases for yourself on lists of one, two, and three values. None of them produce the right answer always.
13:40:38
jcowan
sorry, screwed up both pastes: you need to prepend (define a '(1)) and (define (s- x y) (- y x))
13:41:14
jcowan
in any case, your `minus` function does what I said needs to be done: it special-cases the unary version.
13:42:11
jcowan
irritating: -adic fails for "monadic" (you need "unary"), but -ary fails for "variary" (you need "variadic").
14:58:27
sham1
Well, monadic does work. After all, the APL people seem to be just fine using that word. Same with dyadic, triadic and so on.
15:26:55
jcowan
If you speak of "monadic functions" out of context, though, you'll probably get a WHUT.