Can Scheme expand a list as arguments? - lisp

Considered that I have a procedure (plus x y) witch takes exactly two args. And now I also have a list which contains two objects like (list 1 2). So, if there's any magic way to expand the list as two arguments. We have a dot notion version, but that isn't what i want. I just want to expand the list to make Scheme believe I passed two arguments instead of a list.
Hope those Ruby codes help:
a = [1, 2]
def plus(x,y); x+y; end
plus(*a)
# See that a is an array and the plus method requires
# exactly two arguments, so we use a star operator to
# expand the a as arguments

(apply your-procedure your-list)

This is Scheme's equivalent code:
(define (plus x y)
(+ x y))
(plus 1 2)
=> 3
(define a (list 1 2))
(apply plus a)
=> 3
The "magic" way to expand the list and pass it as arguments to the procedure, is using apply. Read more about it your interpreter's documentation.

Related

Using variable-arity function on values-expression

I ran into this toy example recently that has me a bit confused. Suppose I define a simple variable-arity function as below. Then, we are totally within our rights to run this both on one argument and on two.
(define (my-func . a) a)
(my-func 1) ; evals to '(1)
(my-func 1 2) ; evals to '(1 2)
The weird bit I've run into is if I try to pass it multiple arguments in one go using values as below:
(my-func (values 1 2))
I'd expect this to evaluate to the same thing as (my-func 1 2), but instead throws an arity-mismatch error, namely that it expected 1 argument but got 2.
Why is it expecting one argument if my-func is variable-arity? Does this have to do with the number of syntactic objects as arguments or is it something else?
In Racket, values construct multiple return values (often abbrev. as multiple values or values). Except the case where it's a single value, values cannot be passed as argument(s). Values can't be kept in a variable. Values are mostly useful when returned from a function, and when used in forms like define-values, etc. See this section of the Racket Reference for more details.
To use the terminology from the above link, (my-func []) is a continuation that only accepts a single value. That's why in your program, it errors with the message "expects one, got two".
What you are trying to do can be accomplished with call-with-values:
(call-with-values (lambda () (values 1 2)) my-func)
Or do:
(let-values [(a b) (values 1 2)]
(my-func a b))
Unfortunately one cannot capture variable number of values into one variable.

Lisp: difference between 2 and (2)

I am just starting to learn lisp and am a bit puzzled as to why the compiler does not evaluate a simple integer enclosed in parentheses to the value of that integer.
In the REPL, when I do (+ 3 2) I get 5, but when I do (+ 3 (2)) I get an error, whereas I think the value of the expression (2) should also be 2. Clearly there is something important here that I am unable to lay my finger on - what's the difference between 2 and (2)? Any help would be most appreciated since this seems fundamental to the way lisp works.
The supported Common Lisp syntax for list forms is specified here: CLHS Section 3.1.2.1.2 Conses as Forms.
There are four supported types of lists as forms:
special forms like (quote foo)
macro forms like (defun foo (a1 a1) (+ a1 a2))
function forms like (+ 1 2)
lambda forms like ((lambda (a) (+ a 1)) 2)
That's all. Other lists can't be evaluated in Common Lisp.
You should also note that there is a difference between a list, and an form. All forms (+ 1 2) are lists, but not all lists (2) are forms.
When you type something at the prompt, that needs to be a form. The first part of a form almost always needs to be some kind of operator.
If you have the REPL prompt, and you type in the following, you will get an error, because it is just a list, not a form:
(2)
What will work is something that tells the REPL to construct a list:
'(2)
'(aardvark)
...which is really just shorthand for:
(quote (2))
(quote (aardvark))
Which means that it actually is still a list that starts with an operator, and is therefore a form.
The following examples will return results:
(+ 1 2)
(+ 1 (+ 2 3))
Basically, the way to think about it is that each element (except the first) in the list is evaluated, then the first element is executed on those elements. So (+ 1 (+ 2 3)) is first evaluated as 1 which results in 1, and then (+ 2 3) which again first has the arguments evaluated before the operator is executed, which means 2 and 3 is fed to +, which results in 5, and then 1 and 5 is fed to +.
Is you say (+ 1 (2)), it tries to evaluate each element after the first, going 1 evaluates to 1, but (2) evaluates to... nothing, because the first element is not an operator.
By the way, I find it helpful to look at multiple books and sources, because if one states something in a way I don't understand, I can always consult another one to see if it makes more sense. I suggest these:
Practical Common Lisp
Lisp Quickstart
A list of a bunch of books and references
Hope that helps!

How do I do anything with multiple return values in racket?

It seems like in order to use multiple return values in Racket, I have to either use define-values or collect them into a list with (call-with-values (thunk (values-expr)) list). In the latter case, why would someone to choose to return multiple values instead of a list, if just have to collect them into a list anyway? Additionally, both of these are very wordy and awkward to work into most code. I feel like I must be misunderstanding something very basic about multiple-return-values. For that matter, how do I write a procedure accepting multiple return values?
Although I may be missing some of the Scheme history and other nuances, I'll give you my practical answer.
First, one rule of thumb is if you need to return more than 2 or 3 values, don't use multiple values and don't use a list. Use a struct. That will usually be easier to read and maintain.
Racket's match forms make it much easier to destructure a list return value -- as easy as define-values:
(define (f)
(list 1 2))
(match-define (list a b) (f))
(do-something-with a b)
;; or
(match (f)
[(list a b) (do-something-with a b)])
If you have some other function, g, that takes a (list/c a b), and you want to compose it with f, it's simpler if f returns a list. It's also simpler if both use a two-element struct. Whereas call-with-values is kind of an awkward hot mess, I think.
Allowing multiple return value is an elegant idea, because it makes return values symmetric with arguments. Using multiple values is also faster than lists or structs (in the current implementation of Racket, although it could work otherwise).
However when readability is a higher priority than performance, then in modern Racket it can be more practical to use a list or a struct, IMHO. Having said that I do use multiple values for one-off private helper functions.
Finally, there's a long, interesting discussion on the Racket mailing list.
Racket doc gives us the quintessential example why, in disguise:
> (let-values ([(q r) (quotient/remainder 10 3)])
(if (zero? r)
q
"3 does *not* divide 10 evenly"))
"3 does *not* divide 10 evenly"
We get two values directly, and use them separately in a computation that follows.
update: In Common Lisp, with its decidedly practical, down-to-the-metal, non-functional approach (where they concern themselves with each extra cons cell allocation), it makes much more sense, especially as it allows one to call such procedures in a "normal" way as well, automatically ignoring the "extra" results, kind of like
(let ([q (quotient/remainder 10 3)])
(list q))
But in Racket this is invalid code. So yeah, it looks like an extraneous feature, better to be avoided altogether.
Using list as the consumer defeats the purpose of multiple values so in that case you could just have used lists to begin with. Multiple values is actually a way of optimization.
Semanticly returning a list and several values are similar, but where you return many values in a list effort goes into creation of cons cells to make the list and destructuring accessors to get the values at the other end. In many cases however, you wouldn't notice the difference in performance.
With multiple values the values are on the stack and (call-with-values (lambda () ... (values x y z)) (lambda (x y z) ...) only checks the number to see if it's correct.. If it's ok you just apply the next procedure since the stack has it's arguments all set from the previous call.
You can make syntactic sugar around this and some popular ones are let-values and SRFI-8 receive is a slightly simpler one. Both uses call-with-values as primitive.
values is handy because it
checks that the number of elements returned is correct
destructures
For example, using
(define (out a b) (printf "a=~a b=~a\n" a b))
then
(let ((lst (list 1 2 3)))
(let ((a (first lst)) (b (second lst))) ; destructure
(out a b)))
will work even though lst has 3 elements, but
(let-values (((a b) (values 1 2 3)))
(out a b))
will not.
If you want the same control and destructuring with a list, you can however use match:
(let ((lst (list 1 2)))
(match lst ((list a b) (out a b))))
Note that he creation of the structure, e.g. (list 1 2) vs (values 1 2) is equivalent.

Generalized Threading Macro in Clojure

Note: this is NOT about concurrency. This is about the thread macro.
I know that -> puts the object at the 2nd position and ->> puts the argument at the last position.
Now, I'm curious, much like the short hand notation of #( ... % ) for functions, is there a short hand notation for threads that lets me place the argument at arbitrary location?
The goal would be that instead of having a fixed location for the thread to run through ... I can write arbitrary forms, and insert %% at special places, and the %% is where the thread gets inserted.
Thanks!
There is now a generalized threading macro in Clojure since 1.5 called as->.
This tweet gives an example of how it works: https://twitter.com/borkdude/status/302881431649128448
(as-> "/tmp" x
(java.io.File. x)
(file-seq x)
(filter (memfn isDirectory) x)
(count x))
First 'x' is bound to "/tmp" and a file is made out of it. 'x' is rebound again to the resulting file and a put through the 'file-seq' function, etc.
The 'diamond wand' from Swiss Arrows library would do what you're asking for:
(-<> 0
(* <> 5)
(vector 1 2 <> 3 4))
; => [1 2 0 3 4]
That said, it isn't something you end up needing often (or ever in my Clojure experience)
In case anyone else comes across this, there is a reason the provided macros exist, but an arbitrary placement one does not: the latter would lead to poor API design.
The -> macro places the argument in the first position. This corresponds to functions that work on some subject argument, e.g., conj, assoc.
The ->> macro places the argument in the last position. This corresponds to functions that work on sequences, e.g., map, reduce.
Design your APIs well, and you'll be less likely to need such a macro.
There was a library that provided this feature, but I forgot where. It might of been in the deprecated clojure-contrib. It was the -$> macro.
But you could derive one from clojure's core -> macro to make the one you're looking for:
(defmacro -$>
([x] x)
([x form] (if (seq? form)
(with-meta (map #(if (= %1 '$) x %1) form) (meta form))
(list form x)))
([x form & more] `(-$> (-$> ~x ~form) ~#more)))
And use $ to indicate the insertion point:
user=> (-$> 2 str (identity $) (println $))
2
nil
Technically, you could use multiple $ in one form. But this implementation suffers from expanding the same form multiple times (in exchange for simplicity).

Why should I use 'apply' in Clojure?

This is what Rich Hickey said in one of the blog posts but I don't understand the motivation in using apply. Please help.
A big difference between Clojure and CL is that Clojure is a Lisp-1, so funcall is not needed, and apply is only used to apply a function to a runtime-defined collection of arguments. So, (apply f [i]) can be written (f i).
Also, what does he mean by "Clojure is Lisp-1" and funcall is not needed? I have never programmed in CL.
Thanks
You would use apply, if the number of arguments to pass to the function is not known at compile-time (sorry, don't know Clojure syntax all that well, resorting to Scheme):
(define (call-other-1 func arg) (func arg))
(define (call-other-2 func arg1 arg2) (func arg1 arg2))
As long as the number of arguments is known at compile time, you can pass them directly as is done in the example above. But if the number of arguments is not known at compile-time, you cannot do this (well, you could try something like):
(define (call-other-n func . args)
(case (length args)
((0) (other))
((1) (other (car args)))
((2) (other (car args) (cadr args)))
...))
but that becomes a nightmare soon enough. That's where apply enters the picture:
(define (call-other-n func . args)
(apply other args))
It takes whatever number of arguments are contained in the list given as last argument to it, and calls the function passed as first argument to apply with those values.
The terms Lisp-1 and Lisp-2 refer to whether functions are in the same namespace as variables.
In a Lisp-2 (that is, 2 namespaces), the first item in a form will be evaluated as a function name — even if it's actually the name of a variable with a function value. So if you want to call a variable function, you have to pass the variable to another function.
In a Lisp-1, like Scheme and Clojure, variables that evaluate to functions can go in the initial position, so you don't need to use apply in order to evaluate it as a function.
apply basically unwraps a sequence and applies the function to them as individual arguments.
Here is an example:
(apply + [1 2 3 4 5])
That returns 15. It basically expands to (+ 1 2 3 4 5), instead of (+ [1 2 3 4 5]).
You use apply to convert a function that works on several arguments to one that works on a single sequence of arguments. You can also insert arguments before the sequence. For example, map can work on several sequences. This example (from ClojureDocs) uses map to transpose a matrix.
user=> (apply map vector [[:a :b] [:c :d]])
([:a :c] [:b :d])
The one inserted argument here is vector. So the apply expands to
user=> (map vector [:a :b] [:c :d])
Cute!
PS To return a vector of vectors instead of a sequence of vectors, wrap the whole thing in vec:
user=> (vec (apply map vector [[:a :b] [:c :d]]))
While we're here, vec could be defined as (partial apply vector), though it isn't.
Concerning Lisp-1 and Lisp-2: the 1 and 2 indicate the number of things a name can denote in a given context. In a Lisp-2, you can have two different things (a function and a variable) with the same name. So, wherever either might be valid, you need to decorate your program with something to indicate which you mean. Thankfully, Clojure (or Scheme ...) allows a name to denote just one thing, so no such decorations are necessary.
The usual pattern for apply type operations is to combine a function provided at runtime with a set of arguments, ditto.
I've not done enough with clojure to be able to be confident about the subtleties for that particular language to tell whether the use of apply in that case would be strictly necessary.
Apply is useful with protocols, especially in conjunction with threading macros. I just discovered this. Since you can't use the & macro to expand interface arguments at compile time, you can apply an unpredictably sized vector instead.
So I use this, for instance, as part of an interface between a record holding some metadata about a particular xml file and the file itself.
(query-tree [this forms]
(apply xml-> (text-id-to-tree this) forms)))
text-id-to-tree is another method of this particular record that parses a file into an xml zipper. In another file, I extend the protocol with a particular query that implements query-tree, specifying a chain of commands to be threaded through the xml-> macro:
(tags-with-attrs [this]
(query-tree this [zf/descendants zip/node (fn [node] [(map #(% node) [:tag :attrs])])])
(note: this query by itself will return a lot of "nil" results for tags that don't have
attributes. Filter and reduce for a clean list of unique values).
zf, by the way, refers to clojure.contrib.zip-filter, and zip to clojure.zip. The xml-> macro is from the clojure.contrib.zip-filter.xml library, which I :use