Hi
I have a simple question.
I want a function that takes input a symbol and returns a list made up
of the two components of the input symbol separated by a character '+'.
We can safely assume that there will be one and only one of these special
characters embedded inside.
Also assume that the character is always there.
I was hoping that
(defun split-on-plus (insym)
(ler ((ret-list nil)
(sym-name (symbol-name insym)))
(push (string-left-trim "+" sym-name) ret-list)
(push (string-right-trim "+" sym-name) ret-list)
ret-list))
I hoped this would do the trick, but I found out that the example given in
CLt:L2
(for string-left-trim) dont work on CLISP and "cmucl".
I tried typing them in and all they were doing was throwing the input
string back at me.
E.g.
(string-left-trim "abc" "labcabcabc") => "labcabc" (It should give that
according to the book)
But I get => "labcabcabc".
So what am I doing wrong ????
I had to write 'split-on-plus' using (aref ) on the string.
I am sure there must be a better way of getting at sub-strings.
Any pointers ???
TIA
Sandeep.
From: Tim Bradshaw
Subject: Re: Q: Function to convert 'abc+def = (abc def)
Date:
Message-ID: <nkj4s12zu86.fsf@tfeb.org>
"Sandeep Koranne" <···············@nospam.philips.com> writes:
> (string-left-trim "abc" "labcabcabc") => "labcabc" (It should give that
> according to the book)
> But I get => "labcabcabc".
> So what am I doing wrong ????
>
That's the right result. STRING-LEFT-TRIM strips all characters in
the `characr bag' which is its first argument from the left of its
second argument until it encounters a character which is not in its
first argument. The first such character in your example is #\l.
I don't see why you're doing it this way though. Surely the technique is:
find the position of the first instance of the char to split on in
the string
extract the subnstring left of it, and the substring right of it,
with SUBSEQ.
--tim
From: Erik Naggum
Subject: Re: Q: Function to convert 'abc+def = (abc def)
Date:
Message-ID: <3183715001067568@naggum.net>
* "Sandeep Koranne" <···············@nospam.philips.com>
| I want a function that takes input a symbol and returns a list made up
| of the two components of the input symbol separated by a character '+'.
| We can safely assume that there will be one and only one of these special
| characters embedded inside.
| Also assume that the character is always there.
|
| I was hoping that
| (defun split-on-plus (insym)
| (ler ((ret-list nil)
| (sym-name (symbol-name insym)))
| (push (string-left-trim "+" sym-name) ret-list)
| (push (string-right-trim "+" sym-name) ret-list)
| ret-list))
Please make an effort to post both tested and readable code.
| I hoped this would do the trick, but I found out that the example
| given in CLt:L2 (for string-left-trim) dont work on CLISP and "cmucl".
| I tried typing them in and all they were doing was throwing the input
| string back at me.
I highly doubt that.
| E.g. (string-left-trim "abc" "labcabcabc") => "labcabc"
| (It should give that according to the book)
Really? Why do you believe that? Please note that the _left_ edge of
this text is in this direction <---, while the _right_ edge of the
text is in that direction --->. You need to read the specification of
these functions, too. The first argument is not a _string_, but a
_collection_ of characters. E.g,, string-right-trim applied to the
same arguments would simply return "l".
| So what am I doing wrong ????
You are _guessing_ and you fail to be aware of it. That's a cardinal
epistemological _sin_, dude. Repent now.
| I had to write 'split-on-plus' using (aref ) on the string.
| I am sure there must be a better way of getting at sub-strings.
Well, position will find the character for you.
(defun silly-function-#398 (symbol)
(let* ((symbol-name (symbol-name symbol))
(delimiter (position #\+ symbol-name)))
(list (intern (subseq symbol-name 0 delimiter))
(intern (subseq symbol-name (1+ delimiter))))))
Not that this necessarily does what you expect, unless you never
change which package is current in your system.
#:Erik
--
ALGORITHM: a procedure for solving a mathematical problem in a finite
number of steps that frequently involves repetition of an operation.
ALGOREISM: a procedure for solving an electoral problem in a finite
number of steps that frequently involves repetition of an operation.
From: Sandeep Koranne
Subject: Re: Q: Function to convert 'abc+def = (abc def)
Date:
Message-ID: <8vbi15$q3l$1@porthos.nl.uu.net>
My mistake.
I completely got confused as to what I was typing in the Lisp reader.
Sorry for wasting your time.
But thanx for the solution anyway.
Regards
Sandeep