For my global TODO list, I am showing breadcrumbs as suggested here :
(concat \"[ \"(org-format-outline-path (org-get-outline-path)) \" ]\") ")
to produce following:
I would like to show only the second level of project breadcrumb. So in this case, I would only display [Project A]. I think if I can make a function that can extract the second level, I just need to prepend with %? so that [Tasks] does not appear for Tasks, but only project names would appear for Projects. What would be an ideal way of extracting the second level?
All you have to do to get the second element of (org-get-outline-path) is to call nth.
(nth N LIST)
Return the Nth element of LIST.
N counts from zero. If LIST is not that long, nil is returned.
The second element is (nth 1 LIST). Replace (org-get-outline-path) with (list (nth 1 (org-get-outline-path))) (we use list because that's what org-format-outline-path expects).
Related
I am studying the Emacs-Lisp by following the introduction.
I can understand the below defun print out a list in left-to-right order because the print command comes before recursion (as I understood):
(defun print-elements-recursively (list)
"Print each element of LIST on a line of its own.
Uses recursion."
(when list ; do-again-test
(print (car list)) ; body
(print-elements-recursively ; recursive call
(cdr list)))) ; next-step-expression
E.g. for a list of '(gazelle giraffe lion tiger). The print order is gazelle, giraffe, lion, tiger.
However, I could not understand why the same order still holds when I switch the position of the two expression within the when body:
(defun print-elements-recursively (list)
"Print each element of LIST on a line of its own.
Uses recursion."
(when list ; do-again-test
; body
(print-elements-recursively ; recursive call
(cdr list))
(print (car list)))) ; next-step-expression
Per my expectation, the recursion happens before the print function, therefore, the order should be reversed. May I know why?
You probably did not evaluate the second defun after defining it, and that is why the items of the input list are still being printed in the original order. Adding a second function with the same name to the global namespace does not mean that the definition of the first function is automatically overwritten.
I suggest you
rename one of the defuns
evaluate them both
and then call each one of them separately.
The behavior should not persist when you do that.
Aside from printing the elements of the list in a different order, note also that the original function returns nil and the second function returns a printed representation of the last (non-nil) item of the input list. This is because (when list) returns nil and is the last expression that gets evaluated when the base case is reached in the first function. In the second function all invocations of print are evaluated after the base case is reached.
Using the built-in map and zip functions, define a Scheme function zipadd that takes two lists of numbers and returns the a list consisting of the corresponding elements added together. You may assume that the lists are the same length. For example (zipadd '(1 2 3) '(4 5 6)) is (5 7 9). (A correct solution not using zip and map will be worth 8 points.)
I am not sure how to do this. I would really like to know the solution before my exam tomorrow. Can anyone help me please?
For starters, Racket doesn't come with a zip procedure, although it's trivial to implement one:
(define (zip lst1 lst2)
(map list lst1 lst2))
Now regarding your question - a solution using only map is the simplest way to solve this problem and it's very similar to the above procedure, just think what could be used to fill the blank:
(define (zipadd lst1 lst2)
(map <???> lst1 lst2))
Although it's a bit more contrived, you can use zip to reach the same solution (and get full marks). Try to figure out how it works - zip sticks together all pairs of elements in both lists, creating a list of two-element lists. Afterwards, map will traverse that list and apply a procedure to each element (remember: each element is a list of two elements), creating a new list with the results:
(define (zipadd lst1 lst2)
(map (lambda (x) <???>) ; x is a list with two numbers, how to add them?
(zip lst1 lst2)))
Finally and for completeness' sake, a solution without using map or zip. It's an implementation of map for the special case where the numbers on both lists must be added pair-wise:
(define (zipadd lst1 lst2)
(if <???> ; if either list is empty (*)
<???> ; then return the empty list
(cons (+ <???> <???>) ; else add the first elements of both lists
(zipadd <???> <???>)))) ; and advance the recursion over both lists
(*) Remember: both lists are assumed to have the same length.
Try to write all the variations of the solution, it'll be interesting to compare different approaches to solve the same problem. And if you have to use map and zip to get full marks then by all means use the second version above, just be aware that's not the simplest nor the most efficient way to express the solution to the problem at hand.
I've seen this used once, but couldn't understand what it does. The reference says that it is
#n=object reads as whatever object has object as its printed representation.
However, that object is labeled by n, a required
unsigned decimal integer, for possible reference by the syntax #n#.
The scope of the label is the expression being read by the outermost
call to read; within this expression, the same label may not appear
twice.
Which to me reads as just 56 randomly selected words of English language... Can you, please, show an example of when this may be used?
In Common Lisp it is used by the reader and the printer.
This way you can label an object in some s-expression and refer to it in a different place in the s-expression.
The label is #someinteger= followed by an s-expression. The integer must be unique. You can't use the label twice within a single s-expression.
The reference to a label is #someinteger#. The integer identifies the s-expression to reference. The label must be introduced, before it can be referenced. The reference can be used multiple times within an s-expression.
This is for example used in reading and printing circular lists or data structures with shared data objects.
Here a simple example:
? '(#1=(1 . 2) (#1#))
reads as
((1 . 2) ((1 . 2)))
Note also this:
? (eq (first *) (first (second *)))
T
It is one identical cons cell.
Let's try a circular list.
Make sure that the printer deals with circular lists and does not print them forever...
? (setf *print-circle* t)
T
Now we are constructing a list:
? (setf l1 (list 1 2 3))
(1 2 3)
We are setting the last cdr to the first cons:
? (setf (cdr (last l1)) l1)
#1=(1 2 3 . #1#)
As you can see above, the printed list gets a label and the last cdr is a reference to that label.
We can also enter a circular list directly by using the same notation. The reader understands it:
? '#1=(1 2 3 . #1#)
#1=(1 2 3 . #1#)
Since we have told the printer to deal with such constructs, we can try the expression from the first example:
? '(#1=(1 . 2) (#1#))
(#1=(1 . 2) (#1#))
Now the printer detects that there are two references to the same cons object.
Is there anyone who can help me to write a function in common LISP that counts the numbers in a list?
The code that I have written is below, but it does not work!
(defun count-numbers(lst)
(let(result()))
(dolist(number lst)
(push number result))
(length result))
For example, when I enter this query "(count'(r 4 f d w 2 3 4 1 z))", I must get 5.
Since it's homework, I'll just give some pointers. First: simplicity. If you are new to Common-Lisp, just use its basic features. For example: recursion. In pure functional style. Think about something like this:
(defun count (list counter)
;; something
)
we first check list. If it's empty, we already checked all the elements, so we return counter. If list is not empty, we
take its first element
we check if it's a number
it's a number! We recursively call count on the rest of the list and with counter = counter + 1
it's not a number! We recursively call count on the rest of the list with counter the same as before
Use (numberp n). It returns T if n is a number, NIL if not.
(defun count-numbers (lst)
(let (result ()))
(dolist (number lst)
(push number result))
(length result))
Check the indentation. Is that what you wanted? Maybe not.
Then you also push all elements to the result list? Is that what you want?
Here is a list of functions on numbers. http://www.lispworks.com/documentation/HyperSpec/Body/c_number.htm
Maybe there is one you need?
This is a good introductory Lisp book for download: http://www.cs.cmu.edu/~dst/LispBook/
I'd say there are a variety of ways to solve this, one would be an imperative loop, like the mostly correct solution already written, a recursive counting function (which is probably the worst way since there's no guarantee in CL that you won't blow the stack), or the functional approach you would probably actually use in production. The last one would be this:
(defun count-numbers (list) (count-if #'numberp list))
I have implemented this function. It is supposed to check the input that we have given to it, and if it is found in the list, a "True" will be shown on the screen. However, it just works for the numbers and if I give it a character I receive an error.
(defun element (x lst)
(dolist (item lst)
(if (= item x) (return t))))
How can I modify it so that it can also look for any characters given to it?
Thanks in advance.
There are several comparison operators. The general ones are eq, eql, equal and equalp. Look them up in the hyperspec.
For objects of specific types, there are often specialized comparators, e.g. string= and char=.
Finally, for list operations, there are functions like member, which can free you from writing loops by hand. They take an optional test parameter, through which you can pass the comparison function.
As you discovered, the = function only works with numbers.
If you try basing your function on find instead, you'll likely find that its default use of the eql function as its test provides the behavior you seek:
(defun element (needle haystack)
(not (null (find needle haystack))))
As alternates to find, you should also study its siblings member and position. In your case, since you only want to distinguish between the item having been found or not, you should choose the function that does the least work. My guess is that position loses here, and that member and find are equivalent; member returns the list from which it extracted the car, whereas find returns the car. In both functions, it's necessary to extract the car.
Easy, use #'eq instead of #'=, thus the 3rd line becomes: (if (eq item x) ...
Alternatively, you could use the built-in #'intersection to check if any of the given items are in the list, thus: (if (not (eq (intersection lst '(x)) nil)))