-
Notifications
You must be signed in to change notification settings - Fork 3
Description
Someone askes on /r/lisp Sub Reddit: Python's approach is much better {{{ x= ( 10 * [a] ) }}} because i don't have to remember the (ad-hoc) name of the function.
I think it would be fun to create code that will make Scheme work like Python. examples of python code:
[1] * 2 * 4 * 5
2 * [2] * 3 * 3
"x" * 2 * 3
10 * "x" * 10 * 2you can put sequence (here list or string) and use multiplication to make the sequence larger. And + works by concatenation of the sequences.
Here is my quick code for two arguments:
(define * (let ((mul *))
(lambda args
(if (= (length args) 2)
(let ((first (car args))
(second (cadr args)))
(cond ((and (list? first) (integer? second))
(apply append (make-list second first)))
((and (list? second) (integer? first))
(apply append (make-list first second)))
(else
(apply mul args))))
(apply mul args)))))
scheme> (* '(1 2) 10)
;; ==> (1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2)
scheme> (* 1 2)
;; ==> 2I think it would be a nice exercise to make * work exactly like in Python with lists, vectors, and strings. and It would be a good example that will show that Scheme is more powerful than python because you can do exactly the same what python have in Scheme but not other way around.
What do you think about including a recipe something like "Python * and + in Scheme" or something similar?
NOTE: if you don't like overwriting builtins it was used by Gerrald Sussman in his talk We Really Don't Know How to Compute! from 2011 Strange Loop.