all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Re: Learners doubt in LISP
  2004-01-08 19:31 Learners doubt in LISP Rajsekar Manokaran
@ 2004-01-08  9:22 ` Joakim Hove
  2004-01-08 10:27 ` Ganesh Swami
  1 sibling, 0 replies; 3+ messages in thread
From: Joakim Hove @ 2004-01-08  9:22 UTC (permalink / raw)



Rajsekar Manokaran <rajsekar_manokaran@yahoo.co.uk> writes:


> (completing-read "Input: " '(("hai" 10) ("bye" 20)) nil t nil)

[...]

> Now this thing seems to return "bye" or "hai"
> How do I access the 10 or 20 that comes together with it?

    (let* ((alist '(("hai" 10) ("bye" 20)))
           (key     (completing-read "Input: alist nil t nil))
           (value   (nth 1 (assoc-string key alist))))

        ;; Key and value defined in this scope.
 
    )


The function (assoc-string) returns the whole pair, and (nth 1 )
selects the last one. (Maybe a function to perform this combination
already exists?)


HTH - Joakim



-- 
  /--------------------------------------------------------------------\
 / Joakim Hove  / hove@bccs.no  /  (55 5) 84076       |                 \
 | Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
 | CMU                                                | 5231 Paradis    |
 \ Thormøhlensgt.55, 5020 Bergen.                     | 55 91 28 18     /
  \--------------------------------------------------------------------/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Learners doubt in LISP
  2004-01-08 19:31 Learners doubt in LISP Rajsekar Manokaran
  2004-01-08  9:22 ` Joakim Hove
@ 2004-01-08 10:27 ` Ganesh Swami
  1 sibling, 0 replies; 3+ messages in thread
From: Ganesh Swami @ 2004-01-08 10:27 UTC (permalink / raw)
  Cc: help-gnu-emacs

>>>>> "RM" == Rajsekar Manokaran <rajsekar_manokaran@yahoo.co.uk> writes:

    RM> I have just started learning lisp.  I use emacs (which was my
    RM> motivation to learn LISP) to compile things.

    RM> There is a function in emacs called completing-read which when
    RM> passed some strings allows the user to select one string out
    RM> of the many passed. I want to allow the user select a string
    RM> and then use the data associated with the string.

    RM> eg.

    RM> (completing-read "Input: " '(("hai" 10) ("bye" 20)) nil t nil)


| C-h f assoc |----------
| 
| assoc is a built-in function.
| (assoc KEY LIST)
| 
| Return non-nil if KEY is `equal' to the car of an element of LIST.
| The value is actually the first element of LIST whose car equals KEY.


Real lisping:

  (defun y-completing-read (x)
    (cadr 
     (assoc 
      (completing-read "Input: " x nil t nil) 
      x)))


Better readability:

  (defun my-completing-read (x)
    (let ((ret (completing-read "Input: " x nil t nil)))
      (cadr (assoc ret x))
      ))
    

(my-completing-read '(("hai" 10) ("bye" 20)))


cheers,
Ganesh


    RM> reads allowing completions hai and bye.

    RM> the nil t nil are insignificant (t - only allow things on the
    RM> list).

    RM> Now this thing seems to return "bye" or "hai" How do I access
    RM> the 10 or 20 that comes together with it?


-- 
Ganesh Swami

If you want to get laid, go to school;
If you want to get educated, go to the library.
       -- Frank Zappa.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Learners doubt in LISP
@ 2004-01-08 19:31 Rajsekar Manokaran
  2004-01-08  9:22 ` Joakim Hove
  2004-01-08 10:27 ` Ganesh Swami
  0 siblings, 2 replies; 3+ messages in thread
From: Rajsekar Manokaran @ 2004-01-08 19:31 UTC (permalink / raw)





I have just started learning lisp.
I use emacs (which was my motivation to learn LISP) to compile things.

There is a function in emacs called completing-read which when passed some
strings allows the user to select one string out of the many passed. I want to
allow the user select a string and then use the data associated with the
string.

eg.

(completing-read "Input: " '(("hai" 10) ("bye" 20)) nil t nil)

reads allowing completions hai and bye.

the nil t nil are insignificant (t - only allow things on the list).

Now this thing seems to return "bye" or "hai"
How do I access the 10 or 20 that comes together with it?

Thanks in advance.

M Rajsekar

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-01-08 19:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-01-08 19:31 Learners doubt in LISP Rajsekar Manokaran
2004-01-08  9:22 ` Joakim Hove
2004-01-08 10:27 ` Ganesh Swami

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.