Search
Friday, 5th of August 2022, 23:51:57 UTC
4:45:47
prokhor__
good morning everyone :)
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:35:39
sham1
apply simply doesn't act like reduce, ever
7:35:57
sham1
The function you're calling with apply might
7:36:03
wasamasa
the closest approach of making that notion work is if you were to do currying
7:36:55
wasamasa
you can translate a function accepting several arguments to a function accepting one argument less
7:37:46
rendar
yes but at some point apply must check function numbers of args? or not?
7:40:59
wasamasa
it can do this for optimization purposes
7:41:07
wasamasa
but you can punt this to the function it ends up calling
7:41:31
sham1
And that's probably better if one had multiple arities
7:41:49
wasamasa
really, you're using python
7:41:50
sham1
Like for example with Scheme's case-lambda or whatever clojure does
7:41:57
wasamasa
return func(*func_args)
7:42:00
wasamasa
it's as easy as this
7:42:12
wasamasa
if you want to expose apply to the lisp interpreter, it's slightly bit more complicated
7:42:37
wasamasa
but in the evaluator, you just need to use the asterisk, no reduce
7:42:51
wasamasa
reduce repeatedly calls a binary function with the accumulated result
7:43:02
wasamasa
which happens to be how + and * behaves, but not how - and / behave
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:51:51
wasamasa
then your implementation of + is broken
7:52:01
wasamasa
it needs to accept an arbitrary amount of args
7:52:23
wasamasa
in scheme (+) returns 0, (+ 1) returns 1, (+ 1 2) returns 3 and (+ 1 2 3) returns 6
7:52:32
rendar
wasamasa, that's it!!
7:52:50
wasamasa
dunno about CL, but scheme lifted its numerical tower from it
7:52:51
rendar
wasamasa, in the underlying python i'm mapping symbol '+' to operator.add
7:52:54
wasamasa
so I'd expect it to be close
7:52:57
rendar
which is a function which accepts 2 args
7:52:58
wasamasa
yeah, that's wrong
7:53:09
rendar
but with functools.reduce, it works
7:53:10
wasamasa
yeah, CL behaves identical
7:53:27
wasamasa
you need to implement + in terms of functools.reduce, not apply in terms of functools.reduce
7:53:43
rendar
wasamasa, check this out https://bpa.st/7A7Q
7:54:02
wasamasa
yeah, I've seen this a few times
7:54:06
rendar
wasamasa, i think i have to do that for every 2-ary functions, right?
7:54:22
wasamasa
no, you need to look at every individual function you need to implement and decide how
7:54:33
wasamasa
you can't just blindly take shortcuts and complain about falling into a pit
7:54:53
wasamasa
what are you trying to implement anyway?
7:54:57
wasamasa
something CL-like?
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:05
rendar
no, something much simpler
7:55:21
wasamasa
I'd still suggest you to look at the semantics of an existing lisp
7:55:27
rendar
a kind of minimal lisp just to insert commands in a system, minimal ability to create functions and macros
7:55:34
wasamasa
well, except newlisp perhaps
7:55:50
rendar
it changes too much?
7:55:53
wasamasa
because it's seriously broken
7:56:06
wasamasa
you can see every single shortcoming of the implementation by reading the docs
7:56:29
wasamasa
things that have been done better in pretty much every lisp I've come across
7:56:48
wasamasa
it's been around for a while, but still firmly stuck in hobbyist stage
7:57:36
wasamasa
that description doesn't say much
7:57:51
wasamasa
you still need to decide how basic arithmetic should behave and how a useful standard library looks like
7:57:59
wasamasa
and for this it doesn't hurt to look at prior art
7:58:11
wasamasa
for example if CL is too much, you could look at islisp
7:58:47
rendar
well, basic arithmetic? doesn't that just follow the ( ) priorities?
7:59:13
wasamasa
you'd be surprised how much there can be to it
7:59:24
wasamasa
we regularly get people asking about the behavior of (+) and (*)
7:59:51
wasamasa
besides, what is every 2-ary function supposed to mean
7:59:58
wasamasa
is cons a 2-ary function because it takes two arguments?
8:01:17
rendar
hmm, well it means every function which takes 2 args, i guess
8:01:31
wasamasa
that is pretty misleading because cons is different from +
8:01:51
wasamasa
2-ary operators maybe and that's limited to a handful
8:27:51
rendar
well, you can't run cons with multiple args
8:33:35
wasamasa
there's list which accepts several args to create a list
8:43:16
sham1
There's also LIST*, and over in Scheme land, cons*
8:43:25
sham1
Well, the latter is in an SRFI
8:58:40
pjb
wasamasa: it's not misleading, + is n-ary.
8:58:55
pjb
wasamasa: cons and truncate are 2-ary: (cons a b) (truncate a b) are valid forms.
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:15
wasamasa
there is generally no must with lisps
9:19:30
wasamasa
sure, you're free to implement it as a dispatch to several functions
9:19:46
wasamasa
but you might as well just have your minus function check for the number of arguments itself
9:20:04
wasamasa
if it's just one, negate the number
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)))))
9:32:58
sham1
Yeah, there are many different ways of reifying this idea of having n-ary functions
9:53:46
rendar
wasamasa, ok, so the checking is postponed to the - func
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.
Saturday, 6th of August 2022, 11:51:57 UTC