* Designing interface of a simple elisp function
@ 2002-10-12 15:39 gnuist006
2002-10-12 16:15 ` Henrik Motakef
2002-10-13 0:06 ` Mario Lang
0 siblings, 2 replies; 4+ messages in thread
From: gnuist006 @ 2002-10-12 15:39 UTC (permalink / raw)
I want to write a function, say converting a binary to decimal in lisp.
For arbitrary length binary, I want the input as a string, which is what
we do in C. Then I want to get "car" of the string and go from there.
This is like getchar or getc in C.
Now this "car" does not apply to a string but to a list. On the other
hand the arbitary length input applies to string. Does there exist
string to list function? But even that also seems cheating. What is the
most elegant way to write such a function so that it is also readible
in use.
(b2d '(1 0 1 0 0 1)) is hardly readible or desirable.
(b2d '101001) is most desirable
(b2d "101001") is tolerable if this is the best that can be done
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Designing interface of a simple elisp function
2002-10-12 15:39 Designing interface of a simple elisp function gnuist006
@ 2002-10-12 16:15 ` Henrik Motakef
2002-10-13 0:24 ` Edward O'Connor
2002-10-13 0:06 ` Mario Lang
1 sibling, 1 reply; 4+ messages in thread
From: Henrik Motakef @ 2002-10-12 16:15 UTC (permalink / raw)
gnuist006@hotmail.com (gnuist006) writes:
> Now this "car" does not apply to a string but to a list. On the other
> hand the arbitary length input applies to string. Does there exist
> string to list function? But even that also seems cheating. What is the
> most elegant way to write such a function so that it is also readible
> in use.
The map-functions work on sequences, not only lists. So, for example
(mapcar 'identity "foo")
returns a list of three characters. An alternative would be
(split-string "foo" "")
which gives a list of one-character strings.
> (b2d '101001) is most desirable
> (b2d "101001") is tolerable if this is the best that can be done
You can convert a symbol to a name with the symbol-name function,
i.e. (symbol-name 'foo) => "foo". Unfortunatly, this will not work
with '101001, because it really is the same as 101001 - numbers are
self-quoting, so for example (eq '0001 1) is t, as is (numberp '1).
You could use '\101001 however, or just accept a number instead of a
symbol or string.
Regards
Henrik
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Designing interface of a simple elisp function
2002-10-12 15:39 Designing interface of a simple elisp function gnuist006
2002-10-12 16:15 ` Henrik Motakef
@ 2002-10-13 0:06 ` Mario Lang
1 sibling, 0 replies; 4+ messages in thread
From: Mario Lang @ 2002-10-13 0:06 UTC (permalink / raw)
gnuist006@hotmail.com (gnuist006) writes:
> I want to write a function, say converting a binary to decimal in lisp.
> For arbitrary length binary, I want the input as a string, which is what
> we do in C. Then I want to get "car" of the string and go from there.
> This is like getchar or getc in C.
> Now this "car" does not apply to a string but to a list. On the other
> hand the arbitary length input applies to string. Does there exist
> string to list function? But even that also seems cheating. What is the
> most elegant way to write such a function so that it is also readible
> in use.
You are looking for aref:
(aref "abc" 1) => 98
C-h f aref RET for more info.
--
CYa,
Mario
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Designing interface of a simple elisp function
2002-10-12 16:15 ` Henrik Motakef
@ 2002-10-13 0:24 ` Edward O'Connor
0 siblings, 0 replies; 4+ messages in thread
From: Edward O'Connor @ 2002-10-13 0:24 UTC (permalink / raw)
On 12 Oct 2002, Henrik Motakef wrote:
> The map-functions work on sequences, not only lists. So, for
> example
>
> (mapcar 'identity "foo")
>
> returns a list of three characters. An alternative would be
>
> (split-string "foo" "")
>
> which gives a list of one-character strings.
You want to be careful with this latter option if you want to
write portable Elisp programs. Some XEmacsen will infloop on that.
--
Edward O'Connor
oconnor@soe.ucsd.edu
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2002-10-13 0:24 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-10-12 15:39 Designing interface of a simple elisp function gnuist006
2002-10-12 16:15 ` Henrik Motakef
2002-10-13 0:24 ` Edward O'Connor
2002-10-13 0:06 ` Mario Lang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).