Dynamic let List Destructuring in Clojure - syntax

I have a let statement in which I would like to dynamically destructure a list. The following is my solution:
symList ;; list of some Strings which will become the vector of Symbols to assign to
valList ;; list of some values, same length as symList
(let [(map read-string symList) valList]
...)
An example value of symList would be ("pt1" "pt2") and an example value of valList would be (1 2)
However, this produces an exception that the first part is an "unsupported binding form". I suspect I am missing something regarding syntax-quoting, or that it's not possible. Any advice would be greatly appreciated.
EDIT: I will only know these values come run time, hence this approach. Secondly, I need to be able to pass the lexical scope later on, hence the use of let.

If symList and valList have the correct value at compilation time, then you could write a macro to do this. If they are only known at runtime, you are going to have to use/write a function to do the destructuring for you and return the result of that destructuring as some data structure.
In the second case, for something simple like this, you can use (zipmap symList valList) to get a map.

(let [valList (map str symList)]
(somefun valList))
The error you are getting is because your let statement was backwards (let [val SOURCE] STUFF)

maybe matchure can give you what you want

Although I've been unable to find a way to dynamically destructure a list, for those of you who are interested in creating values that are lexically instead of dynamically scoped, I found that intern to whatever namespace you need works well.

Related

How to determine if a variable exists in Chicken Scheme?

Is there a way in Chicken Scheme to determine at run-time if a variable is currently defined?
(let ((var 1))
(print (is-defined? var)) ; #t
(print (is-defined? var)) ; #f
EDIT: XY problem.
I'm writing a macro that generates code. This generated code must call the macro in mutual recursion - having the macro simply call itself won't work. When the macro is recursively called, I need it to behave differently than when it is called initially. I would use a nested function, but uh....it's a macro.
Rough example:
(defmacro m (nested)
(if nested
BACKQUOTE(print "is nested")
BACKQUOTE(m #t)
(yes, I know scheme doesn't use defmacro, but I'm coming from Common Lisp. Also I can't seem to put backquotes in here without it all going to hell.)
I don't want the INITIAL call of the macro to take an extra argument that only has meaning when called recursively. I want it to know by some other means.
Can I get the generated code to call a macro that is nested within the first macro and doesn't exist at the call site, maybe? For example, generating code that calls (,other-macro) instead of (macro)?
But that shouldn't work, because a macro isn't a first-class object like a function is...
When you write recursive macros I get the impression that you have an macro expansion (m a b ...) that turns into a (m-helper a (b ...)) that might turn into (let (a ...) (m b ...)). That is not directly recursive since you are turning code into code that just happens to contain a macro.
With destructuring-bind you really only need to keep track of two variables. One for car and one for cdr and with an implicit renaming macro the stuff not coming from the form is renamed and thus hygenic:
(define-syntax destructuring-bind
(ir-macro-transformer
(lambda (form inject compare?)
(define (parse-structure structure expression optional? body)
;;actual magic happens here. Returns list structure with a mix of parts from structure as well as introduced variables and globals
)
(match form
[(structure expression) . body ]
`(let ((tmp ,expression))
,(parse-structure structure 'tmp #f body))))))
To check if something from input is the same symbol you use the supplied compare? procedure. eg. (compare? expression '&optional).
There's no way to do that in general, because Scheme is lexically scoped. It doesn't make much sense to ask if a variable is defined if an referencing an undefined variable is an error.
For toplevel/global variables, you can use the symbol-utils egg but it is probably not going to work as you expect, considering that global variables inside modules are also rewritten to be something else.
Perhaps if you can say what you're really trying to do, I can help you with an alternate solution.

Difference between multiple values and plain tuples in Racket?

What is the difference between values and list or cons in Racket or Scheme? When is it better to use one over the other? For example, what would be the disadvantage if quotient/remainder returns (cons _ _) rather than (values _ _)?
Back in 2002 George Caswell asked that question in comp.lang.scheme.
The ensuing thread is long, but has many insights. The discussion
reveals that opinions are divided.
https://groups.google.com/d/msg/comp.lang.scheme/ruhDvI9utVc/786ztruIUNYJ
My answer back then:
> What are the motivations behind Scheme's multiple return values feature?
> Is it meant to reflect the difference in intent, or is there a
> runtime-practical reason?
I imagine the reason being this.
Let's say that need f is called by g. g needs several values from f.
Without multiple value return, f packs the values in a list (or vector),
which is passed to g. g then immediately unpacks the list.
With multple values, the values are just pushed on the stack. Thus no
packing and unpacking is done.
Whether this should be called an optimization hack or not, is up to you.
--
Jens Axel Søgaard
We don't need no side-effecting We don't need no allocation
We don't need no flow control We don't need no special-nodes
No global variables for execution No dark bit-flipping for debugging
Hey! did you leave the args alone? Hey! did you leave those bits alone?
(Chorus) -- "Another Glitch in the Call", a la Pink Floyd
They are semantically the same in Scheme and Racket. In both you need to know how the return looks like to use it.
values is connected to call-with-values and special forms like let-values are just syntax sugar with this procedure call. The user needs to know the form of the result to use call-with-values to make use of the result. A return is often done on a stack and a call is also on a stack. The only reason to favor values in Scheme would be that there are no overhead between the producer return and the consumer call.
With cons (or list) the user needs to know how the data structure of the return looks like. As with values you can use apply instead of call-with-values to do the same thing. As a replacement for let-values (and more) it's easy to make a destructuring-bind macro.
In Common Lisp it's quite different. You can use values always if you have more information to give and the user can still use it as a normal procedure if she only wants to use the first value. Thus for CL you wouldn't need to supply quotient as a variant since quotient/remainder would work just as well. Only when you use special forms or procedures that take multiple values will the fact that the procedure does return more values work the same way as with Scheme. This makes values a better choice in CL than Scheme since you get away with writing one instead of more procedures.
In CL you can access a hash like this:
(gethash 'key *hash* 't)
; ==> T; NIL
If you don't use the second value returned you don't know if T was the default value or the actual value found. Here you see the second value indicating the key was not found in the hash. Often you don't use that value if you know there are only numbers the default value would already be an indication that the key was not found. In Racket:
(hash-ref hash 'key #t)
; ==> #t
In racket failure-result can be a thunk so you get by, but I bet it would return multiple values instead if values did work like in CL. I assume there is more housekeeping with the CL version and Scheme, being a minimalistic language, perhaps didn't want to give the implementors the extra work.
Edit: Missed Alexis' comment on the same topic before posting this
One oft-overlooked practical advantage of using multiple return values over lists is that Racket's compose "just works" with functions that return multiple values:
(define (hello-goodbye name)
(values (format "Hello ~a! " name)
(format "Goodbye ~a." name)))
(define short-conversation (compose string-append hello-goodbye))
> (short-conversation "John")
"Hello John! Goodbye John."
The function produced by compose will pass the two values returned by hello-goodbye as two arguments to string-append. If you're writing code in a functional style with lots of compositions, this is very handy, and it's much more natural than explicitly passing values around yourself with call-with-values and the like.
It's also related to your programming style. If you use values, then it usually means you want to explicitly return n values. Using cons, list or vector usually means you want to return one value which contains something.
There are always pros/cons. For values: It may use less memory on some implemenentations. The caller need to use let-values or other multiple values specific syntax. (I wish I could use just let like CL.)
For cons or other types: You can use let or lambda to receive the returning value. You need to explicitly deconstruct it to get the value you want using car or other procedures.
Which to use and when? Again depending on your programming style and case by case but if the returning value can't be represented in one object (e.g. quotient and remainder), then it might be better to use values to make the procedure's meaning clearer. If the returning value is one object (e.g. name and age for a person), then it might be better to use cons or other constructor (e.g. record).

Equivalent of python 'pass' in drracket

Does anyone know if DrRacket has got an equivalent of Python's pass statement or any other idiom that can be used to instruct the executing code to do nothing?
If you want to write an empty statement (one with no useful result), maybe this will work for you:
(void)
... But it'd be better if you demonstrated with an example what exactly is that you want to do. Anyway, here's a link to the appropriate section in the documentation.
Here is a way to do your hash example.
(hash-ref (hash) "not there" void)
But now you have to check whether you get back a void or a value you want. You may also be interested in hash-update! or hash-update (docs), which combines checking for an existing key with a default behavior for the case where the key does not exist.
As I write this answer, your question is kind of spread among (a) your original question and (b) your comment to Oscar's answer:
Its a case of wanting to indexing a hash map with a key that may or may not exist so if a no key found error returns, I just want to ignore the error and continue with the execution.
Taking that last part literally: The general way to handle -- and effectively "ignore" -- an exception is with-handlers:
(hash-ref (hash) "nothing")
;; hash-ref: no value found for key
;; key: "nothing"
(with-handlers ([exn:fail? (lambda (exn)
"hum dee dum")])
(hash-ref (hash) "nothing"))
;; "hum dee dum"
This doesn't seem to have anything to do with Python pass as described here, but maybe pass can be used to ignore errors in Python; I just don't know it well enough
If you can deal with a syntactic form, then begin works.
(if <conditional> (begin))
if you need a function then this works
(lambda ignore #f)
And you might just avoid the need for 'something that does nothing' by reworking your logic leading to the 'nothing'.

basic Clojure syntax

Let's say I have a macro, inside the macro I have this let:
let[
elements# //stuff//
#newlist (for [e# elements#] (if (number? e#) (**add e# to #newlist**)))
]
Since I am having a really hard time finding proper information on the really simple Clojure stuff, I'm here to ask: what do I need to do to accomplish this task above? I think it should be possible to do this but I don't know the most crucial part!
It looks like you're trying to create a list called newlist which contains all those elements of elements that are numbers. If so, you can just use the filter function like this:
(let
[elements whatever
newlist (filter number? elements)]
do-stuff-with-newlist)
filter takes a predicate function and a list and returns a new list that contains those items for which the predicate returns a true result.
Note that your initial idea of adding to a list inside a for-loop would not work because lists are immutable, so you can not add to them in-place. You could re-assign a variable holding the list inside a for loop (something like (set! my-list (cons e my-list)), but that would not be idiomatic Clojure code. It would also create the list in reverse.

Identifiers and Binding in Scheme - how to interpret the function?

I am reading DrRacket document http://docs.racket-lang.org/guide/binding.html
There is a function
(define f
(lambda (append)
(define cons (append "ugly" "confusing"))
(let ([append 'this-was])
(list append cons))))
> (f list)
'(this-was ("ugly" "confusing"))
I see that we define function f, inside we define lambda that takes (append), why ?
Procedure (body) for lambda is another function called cons, that appends two strings.
I don't understand this function at all.
Thanks !
The section that you're referring to demonstrates lexical scope in Racket. As in other Scheme implementations, the main point is that you can "shadow" every binding in the language. Unlike most "mainstream" languages, there are no real keywords that are "sacred" in the sense that they can never be shadowed by a local binding.
Note that a really good tool to visualize what is bound where is DrRacket's "check syntax" button: click it, and you'll see your code with highlights that shows which parts are bindings, which are special forms -- and if you hover the mouse over a specific name, you'll see an arrow that tells you where it came from.
Scheme takes some getting used to :)
f is assigned the function returned by the lambda.
lambda defines the function that takes a parameter (called append).
(define cons (append "ugly" "confusing")) is not a function per se, but calls append with the two strings as parameter and assigns the result to cons.
inside the let block, append is re-assigned a different value, the symbol this-was.
the let block creates a list of append (which now contains 'this-was) and cons (which contains '("ugly" "confusing") from 3 above
since 5 is the last statement that value is returned by the whole function which is called f
f is called with the parameter list (the list function). which gets passed as the parameter append. And this is why 3 above creates a list '("ugly" "confusing") which gets assigned to cons.
Hope that cleared up things a bit.
Cheers!

Resources