How could I use the sort-regexp-fields to emulate the sort-numeric-fields. I tried like this:
123
345
90
80
1999
M-x sort-regexp-fields
Regexp specifying records to sort: \ ([0-9]+\)
Regexp specifying key within record: \, \#1
But it can’t work.
what I really want to sort is something like this:
foo index: 123
index: 345 boo
…
the question is: I want to sort the lines by the number after the word “index”, but the number is not in the same field, they just have a word “index” before it. what should I do to complete this? anybody can help me?
sort-regexp-fields doesn't do lexicographical compare, and there's no trivial way to get it to do that. That said, we can use a trick of defining our own command, and using that command to signal to some advice to shoe-horn in a lexicographical compare.
The following command does what you want, provided you mark the region around the lines you want to compare:
(defun my-line-sort (begin end)
(interactive "r")
(sort-regexp-fields nil "^.*: +\\([0-9]+\\).*$" "\\1" begin end))
(defun compare-buffer-substrings-lexigraphically (b1 b2)
(< (string-to-number (buffer-substring-no-properties (car b1) (cdr b1)))
(string-to-number (buffer-substring-no-properties (car b2) (cdr b2)))))
(defadvice sort-subr (before sort-subr-lexical-compare activate)
"In special case, force predicate to be a lexical compare"
(when (eq this-command 'my-line-sort)
(ad-set-arg 5 'compare-buffer-substrings-lexigraphically)))
If I understand your situation correctly, this will do what you want:
M-xsort-regexp-fieldsRETindex: \([0-9]+\)RET\1RET
Related
I'm writing code where the user picks from a list of choices. I have defined my list in the following:
;contains the options the user can pick
(define choices (list "choice1" "choice2" "choice3" "choice4" "choice5" "choice6" "OPENING VALUE" "HIGHEST VALUE" "LOWEST VALUE" "CLOSING VALUE" "VOLUME OF SHARES" "ADJUSTED CLOSING VALUE"))
My button gets the name from the list from the following code(only showing one example). In this case it takes the third item from the list:
[label (list-ref choices 2)]
and when I want to change the name then I use the line of code:
(send choice-4-9 set-label (list-ref choices 9))
My prof commented that I should bind names to 6 7 etc so your code would be readable. I'm still a little confused on what he meant by that and how I would do so.
He means for each index, define an identifier bound to that index, ideally named after what it means, e.g.:
(define choice1 0)
(define choice2 1)
(define choice3 2)
....
So now you can write [label (list-ref choices choice3)] instead of [label (list-ref choices 2)]. The code is more readable, and easier to change if necessary because you can change the binding of the identifier rather than every place where the number appears in code.
Incidentally, what you're doing now is called using "magic numbers."
The previous answer is good and I have upvoted it. I only want to mention that a common data structure for something like this is an association list, where you associate something like a symbol or number to a value, and then look it up using assq, assv, or assoc depending on the whether your lookup name requires eq?, eqv?, or equal? respectively. Consider:
(define choices
'((shoot . "Shoot!")
(run . "Run away!")
(reload . "Reload")
(appraise . "Search your surroundings")))
(define (get-label choice)
(let ((result (assq choice choices)))
(if (not result)
(error "???") ; handle as you see fit
(cdr result))))
;;;;;;;;;;;;;;;;
Welcome to DrRacket, version 6.4 [3m].
Language: racket/base [custom]; memory limit: 8192 MB.
> (get-label 'shoot)
"Shoot!"
>
I'm making a random sentence generator using Scheme (Pretty Big), and I'm having trouble defining temporary variables. I want to make something like this:
<NOUN1> <VERB1> <NOUN2> <but> <NOUN2> <VERB1> <NOUN2> <also>
Example: Sharks eat fish, but fish eat fish also.
I have word lists, and functions to choose a word from said list. Then, I use append to create a function. I am able to do:
(define (sentence)
(append (getNoun) '(and) (getNoun) (getVerb)))
However, I am unable to figure out a way to temporarily define a variable.
I have this so far:
(define (sentence1)
(append (getNoun)
(lambda (verb getVerb)
(noun getNoun))
(verb) (noun) '(but) (noun) (verb) (noun)))
Hints/Help please?
You are looking for let.
http://docs.racket-lang.org/reference/let.html
Here is an example usage:
(define (my-proc age)
(let ([age-plus-10 (+ age 10)])
(printf "age is ~a" age)
(printf "age-plus-10 is ~a" age-plus-10)))
Notice how we can temporarily define age-plus-10 and then use it later.
Let's call this function "dynamic-define".
Basically I want to write a macro or lambda that works like this:
$ (dynamic-define "variable" 123)
$ variable
$ => 123
I tried it:
(define-syntax dynamic-define
(syntax-rules ()
((_ string <body>)
(eval `(define ,(string->symbol string) <body>)))))
and it works as expected but doesn't seem to be a good solution.
I tried to use without eval like this:
(define-syntax dynamic-define
(syntax-rules ()
((_ string <body>)
(define (string->symbol string) <body>))))
But when I try to use I get this error:
Error: invalid syntax (define (string->symbol "variable") 123)
What should I do?
(PLEASE: edit this question to fit native English and erase this line please)
You're running against a fundamental problem: you cannot do a real dynamic define in a "clean" way, hence your attempt using eval. Note that the two solutions above are both not dynamic -- all they give you is the ability to write "foo" instead of foo. As I said elsewhere, using eval is usually bad, and in this case it's worse since it's eval that is used from a macro which makes it very weird. This is in addition to having an implicit quasi-quote in your macro that makes things even more confusing. You could just as well define the whole thing as a plain function:
(define (dynamic-define string <body>)
(eval `(define ,(string->symbol string) ,<body>)))
If you really need this to work, then it's best to take a step back and think about what it is that you need, exactly. On one hand, the above solutions are not dynamic since they require using the macro with a string syntax
(dynamic-define "foo" 123) ; instead of (define foo 123)
but how would it look to have a real dynamic definition that takes in a string? You'd probably expect this to define b:
(define a "b")
(dynamic-define a 123)
but this only becomes more confusing when you consider how it interacts with other bindings. For example:
(define y 123)
(define (foo s)
(define x 456)
(dynamic-define s 789)
(+ x y))
Now, assuming that dynamic-define does what you want it to do, think about what you'd get with (foo "x") and (foo "y"). Worse, consider (foo "s") and (foo "+"). And even worse, what about
(foo (random-element '("x" "y" "s" "+" "define")))
? Clearly, if this dynamic-define really does some dynamic definition, then there is no sense that you can make of this code without knowing ahead of time what name foo will be called with. Without being able to make such sense, compilation goes out the window -- but much more importantly, correctness (or generally, the meaning of your code) dies.
In my experience, this kind of pattern is much better handled with hash tables and similar devices.
using define-macro
#> (define-macro (dynamic-define varstr val)
`(define ,(string->symbol varstr) ,val))
#> (dynamic-define "variable" 123)
#> variable
123
using syntax-case
#> (define-syntax dynamic-define
(lambda (stx)
(syntax-case stx ()
((k varstr val)
(with-syntax ([var (datum->syntax-object
#'k
(string->symbol (syntax-object->datum #'varstr)))])
#'(define var val))))))
#> (dynamic-define "variable" 123)
#> variable
123
Can anyone tell me what is the null value representation in mit-scheme? In the SICP book, it should be "nil" but it doesn't work.
Thanks.
'() should work. Basically, nil in scheme is the empty list, so quoting an empty list gives you nil.
There is the history.
visit http://web.archive.org/web/20070808004043/http://wiki.wordaligned.org/sicp/published/FrequentlyAskedQuestions
Original(broken): http://wiki.wordaligned.org/sicp/published/FrequentlyAskedQuestions
Text from the above FAQ (in case of Internet Archive breakage):
Why isn’t “nil” working?
The quick answer is: nil is no longer part of Scheme, use '() instead. The long answer follows…
Early examples in Chapter 2 use nil as a list terminator, but when these examples are run using (e.g.) MIT Scheme, you get:
;Unbound variable: nil
Similarly, using () or null in place of nil may work on some implementations, but neither is portable. The Wizard Book addresses the issue in this footnote
It’s remarkable how much energy in the standardization of Lisp dialects has been dissipated in arguments that are literally over nothing: Should nil be an ordinary name? Should the value of nil be a symbol? Should it be a list? Should it be a pair? In Scheme, nil is an ordinary name, which we use in this section as a variable whose value is the end-of-list marker (just as true is an ordinary variable that has a true value). Other dialects of Lisp, including Common Lisp, treat nil as a special symbol. The authors of this book, who have endured too many language standardization brawls, would like to avoid the entire issue. Once we have introduced quotation in section 2.3, we will denote the empty list as ‘() and dispense with the variable nil entirely.
Since this was written, nil has been excised from the Scheme standard—but the bottom line holds: use ‘(), not nil. In an email to the accu-sicp list, Mike notes:
It’s a troublesome thing, this nil/null/'() business. Scheme48 and
scm don't define null, and guile defines it but as a procedure akin to
common lisp's null (to go with its nil which behaves like cl's nil,
which itself is distinct from '()—maybe this had something to do
with fsf's plans to re-do emacs in guile). I think the best bet is to
replace the authors’ use of nil with ‘().
[Copied with just a touch of typographical clean up, to better match Markdown and usual ASCII input into the languages/implementations in question.]
I use MIT/GNU Scheme microcode 15.3, () and '() all works. (as you said, nil and null doesn't work).
1 ]=> ()
;Value: ()
1 ]=> '()
;Value: ()
1 ]=> (cons 1 ())
;Value 2: (1)
1 ]=> (cons 1 '())
;Value 3: (1)
(list ) '() and () can represent null. for example,
(define (transpose mat) (accumulate-n cons () mat))
or substitute () with '() or (list ).
I want to make emacs indent ruby method calls like:
foo(
:blah => 'bar',
:shibby => 'baz'
)
The closest I can get is:
foo(
:blah => 'bar',
:shibby => 'baz'
)
This is using ruby-deep-indent-paren, ruby-deep-indent-paren-style, ruby-deep-arglist all set to nil.
Hashes indent how I like... if I could just make method calls indent like hashes I would be happy. Any ideas?
Dmitry Gutov has posted this fix, using advice, which seems to work:
(defadvice ruby-indent-line (after unindent-closing-paren activate)
(let ((column (current-column))
indent offset)
(save-excursion
(back-to-indentation)
(let ((state (syntax-ppss)))
(setq offset (- column (current-column)))
(when (and (eq (char-after) ?\))
(not (zerop (car state))))
(goto-char (cadr state))
(setq indent (current-indentation)))))
(when indent
(indent-line-to indent)
(when (> offset 0) (forward-char offset)))))
Ruby indentation in the current Emacs trunk (to be released as 24.4) works like you're asking without any additional tweaks.
I believe there is a key sequence like C-c o, that you could press with the cursor on that closing paren that would show what variable is used and let you type in a new value (like 0 or +). Try that!