From: Anton V. Belyaev
Subject: Simple program: who can write a shorter code?
Date: 
Message-ID: <1183391806.292320.222500@n60g2000hse.googlegroups.com>
I encountered a simple task: to write a function, which takes a string
and outputs ASCII codes of its characters, separated with dots.

Example:

Input: "ablahblah"
Output: "97.108.97.98.108.97.98.108.97.107.112.111.120"

In Python it would be (not for holywars, just to make the task
cleaner):

".".join([str(ord(c)) for c in "blablablakpox"])

How do I write this in Common Lisp?

From: Zach Beane
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <m3abuewzx0.fsf@unnamed.xach.com>
"Anton V. Belyaev" <·············@gmail.com> writes:

> I encountered a simple task: to write a function, which takes a string
> and outputs ASCII codes of its characters, separated with dots.
> 
> Example:
> 
> Input: "ablahblah"
> Output: "97.108.97.98.108.97.98.108.97.107.112.111.120"
> 
> In Python it would be (not for holywars, just to make the task
> cleaner):
> 
> ".".join([str(ord(c)) for c in "blablablakpox"])
> 
> How do I write this in Common Lisp?

(format nil "~{~A~^.~}" (map 'list 'char-code "ablahblah")) 
=> 98.97.100.32.116.104.114.101.97.100.32.97.104.101.97.100

Zach
From: Ralf Mattes
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <f6b83f$t32$1@news01.versatel.de>
On Mon, 02 Jul 2007 15:56:46 +0000, Anton V. Belyaev wrote:

> I encountered a simple task: to write a function, which takes a string
> and outputs ASCII codes of its characters, separated with dots.
> 
> Example:
> 
> Input: "ablahblah"
> Output: "97.108.97.98.108.97.98.108.97.107.112.111.120"
> 
> In Python it would be (not for holywars, just to make the task
> cleaner):
> 
> ".".join([str(ord(c)) for c in "blablablakpox"])
> 
> How do I write this in Common Lisp?

(format NIL "~{~A~^.~}" (mapcar #'char-code (coerce  "test" 'list)))

HTH Ralf Mattes
From: Maciek Pasternacki
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <20070702180729.5d9a32b7@localhost>
On Mon, 02 Jul 2007 15:56:46 -0000
"Anton V. Belyaev" <·············@gmail.com> wrote:

> Input: "ablahblah"
> Output: "97.108.97.98.108.97.98.108.97.107.112.111.120"
> 
> In Python it would be (not for holywars, just to make the task
> cleaner):
> 
> ".".join([str(ord(c)) for c in "blablablakpox"])
> 
> How do I write this in Common Lisp?

#v+
CL-USER> (format nil "~{~D~^.~}" (map 'list #'char-code "ablahblah"))
"97.98.108.97.104.98.108.97.104"
CL-USER> 
#v-

What do I win? ;)

-- 
__    Maciek Pasternacki <·······@japhy.fnord.org> [ http://japhy.fnord.org/ ]
`| _   |_\  / { ...I choose to live and to grow, take and give and to 
,|{-}|}| }\/ move, learn and love and to be paranoid and to lie, hate and fear
\/   |____/ and to do what it takes to move through.. } ( M. J. Keenan )  -><-
From: Anton V. Belyaev
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <1183392732.289993.271570@c77g2000hse.googlegroups.com>
On 2    , 20:07, Maciek Pasternacki <·······@japhy.fnord.org> wrote:
> On Mon, 02 Jul 2007 15:56:46 -0000
> "Anton V. Belyaev" <·············@gmail.com> wrote:
>
> > Input: "ablahblah"
> > Output: "97.108.97.98.108.97.98.108.97.107.112.111.120"
>
> > In Python it would be (not for holywars, just to make the task
> > cleaner):
>
> > ".".join([str(ord(c)) for c in "blablablakpox"])
>
> > How do I write this in Common Lisp?
>
> #v+
> CL-USER> (format nil "~{~D~^.~}" (map 'list #'char-code "ablahblah"))
> "97.98.108.97.104.98.108.97.104"
> CL-USER>
> #v-
>
> What do I win? ;)
>
> --
> __    Maciek Pasternacki <·······@japhy.fnord.org> [http://japhy.fnord.org/]
> `| _   |_\  / { ...I choose to live and to grow, take and give and to
> ,|{-}|}| }\/ move, learn and love and to be paranoid and to lie, hate and fear
> \/   |____/ and to do what it takes to move through.. } ( M. J. Keenan )  -><-

Really cool! :) Thanks.
I should study "format" better!
From: Ralf Mattes
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <f6b8h1$t32$2@news01.versatel.de>
On Mon, 02 Jul 2007 16:12:12 +0000, Anton V. Belyaev wrote:

> On 2    , 20:07, Maciek Pasternacki <·······@japhy.fnord.org> wrote:
>> On Mon, 02 Jul 2007 15:56:46 -0000
>> "Anton V. Belyaev" <·············@gmail.com> wrote:
>>
>> > Input: "ablahblah"
>> > Output: "97.108.97.98.108.97.98.108.97.107.112.111.120"
>>
>> > In Python it would be (not for holywars, just to make the task
>> > cleaner):
>>
>> > ".".join([str(ord(c)) for c in "blablablakpox"])
>>
>> > How do I write this in Common Lisp?
>>
>> #v+
>> CL-USER> (format nil "~{~D~^.~}" (map 'list #'char-code "ablahblah"))
>> "97.98.108.97.104.98.108.97.104"
>> CL-USER>
>> #v-
>>
>> What do I win? ;)
>>
>> --

> Really cool! :) Thanks.
> I should study "format" better!

BTW, our problem is underspecified - and your Python code exposes this:

 What happens if the input string contains non-ASCII characters (i.e.
 charcters with codepoints > 127)?

 In [6]: ".".join([str(ord(c)) for c in u"Frühkartoffel"])
 Out[6]: '70.114.195.188.104.107.97.114.116.111.102.102.101.108'
                 ^^^^^^^

 CL-USER> (format NIL "~{~A~^.~}" (map 'list #'char-code "Frühkartoffel"))
 "70.114.252.104.107.97.114.116.111.102.102.101.108"


YMMV 

 Cheers, Ralf Mattes
From: Anton V. Belyaev
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <1183393416.919154.286950@n60g2000hse.googlegroups.com>
On 2    , 20:18, Ralf Mattes <····@mh-freiburg.de> wrote:
> On Mon, 02 Jul 2007 16:12:12 +0000, Anton V. Belyaev wrote:
> > On 2    , 20:07, Maciek Pasternacki <·······@japhy.fnord.org> wrote:
> >> On Mon, 02 Jul 2007 15:56:46 -0000
> >> "Anton V. Belyaev" <·············@gmail.com> wrote:
>
> >> > Input: "ablahblah"
> >> > Output: "97.108.97.98.108.97.98.108.97.107.112.111.120"
>
> >> > In Python it would be (not for holywars, just to make the task
> >> > cleaner):
>
> >> > ".".join([str(ord(c)) for c in "blablablakpox"])
>
> >> > How do I write this in Common Lisp?
>
> >> #v+
> >> CL-USER> (format nil "~{~D~^.~}" (map 'list #'char-code "ablahblah"))
> >> "97.98.108.97.104.98.108.97.104"
> >> CL-USER>
> >> #v-
>
> >> What do I win? ;)
>
> >> --
> > Really cool! :) Thanks.
> > I should study "format" better!
>
> BTW, our problem is underspecified - and your Python code exposes this:
>
>  What happens if the input string contains non-ASCII characters (i.e.
>  charcters with codepoints > 127)?
>
>  In [6]: ".".join([str(ord(c)) for c in u"Fr�hkartoffel"])
>  Out[6]: '70.114.195.188.104.107.97.114.116.111.102.102.101.108'
>                  ^^^^^^^
>
>  CL-USER> (format NIL "~{~A~^.~}" (map 'list #'char-code "Fr�hkartoffel"))
>  "70.114.252.104.107.97.114.116.111.102.102.101.108"
>
> YMMV
>
>  Cheers, Ralf Mattes

Ok, lets assume that there are only ascii characters.

Is there any way to utilize "string-to-octets" function? The problem
is that it gives out an array, not a list.
From: Edi Weitz
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <usl86n4ud.fsf@agharta.de>
On Mon, 02 Jul 2007 16:23:36 -0000, "Anton V. Belyaev" <·············@gmail.com> wrote:

> Is there any way to utilize "string-to-octets" function? The problem
> is that it gives out an array, not a list.

By now you should have seen several applications of MAP which convert
an array to a list.  And there was also one solution using COERCE.


-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: ············@gmail.com
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <1183450846.806950.163520@k29g2000hsd.googlegroups.com>
On Jul 2, 6:18 pm, Ralf Mattes <····@mh-freiburg.de> wrote:
> On Mon, 02 Jul 2007 16:12:12 +0000, Anton V. Belyaev wrote:
> > On 2    , 20:07, Maciek Pasternacki <·······@japhy.fnord.org> wrote:
> >> On Mon, 02 Jul 2007 15:56:46 -0000
> >> "Anton V. Belyaev" <·············@gmail.com> wrote:
>
> >> > Input: "ablahblah"
> >> > Output: "97.108.97.98.108.97.98.108.97.107.112.111.120"
>
> >> > In Python it would be (not for holywars, just to make the task
> >> > cleaner):
>
> >> > ".".join([str(ord(c)) for c in "blablablakpox"])
>
> >> > How do I write this in Common Lisp?
>
> >> #v+
> >> CL-USER> (format nil "~{~D~^.~}" (map 'list #'char-code "ablahblah"))
> >> "97.98.108.97.104.98.108.97.104"
> >> CL-USER>
> >> #v-
>
> >> What do I win? ;)
>
> >> --
> > Really cool! :) Thanks.
> > I should study "format" better!
>
> BTW, our problem is underspecified - and your Python code exposes this:
>
>  What happens if the input string contains non-ASCII characters (i.e.
>  charcters with codepoints > 127)?
>
>  In [6]: ".".join([str(ord(c)) for c in u"Fr�hkartoffel"])
>  Out[6]: '70.114.195.188.104.107.97.114.116.111.102.102.101.108'
>                  ^^^^^^^
>
>  CL-USER> (format NIL "~{~A~^.~}" (map 'list #'char-code "Fr�hkartoffel"))
>  "70.114.252.104.107.97.114.116.111.102.102.101.108"
>
> YMMV
>
>  Cheers, Ralf Mattes

But this highly depends on the shell locale, otherwise
> cat foo.lisp
#!/usr/local/bin/clisp

(format *terminal-io* "~{~A~^.~}~%" (map 'list #'char-code (read-line
*terminal-io* nil))))
>
> echo "Fr�hkartoffel" | foo.lisp
*** - invalid byte #xFC in CHARSET:ASCII conversion

Or do you have any tip to avoid this ?

-Nicolas
From: Pascal Bourguignon
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <87lkdxagx8.fsf@informatimago.com>
············@gmail.com writes:

>> cat foo.lisp
> #!/usr/local/bin/clisp
>
> (format *terminal-io* "~{~A~^.~}~%" (map 'list #'char-code (read-line
> *terminal-io* nil))))
>>
>> echo "Fr�hkartoffel" | foo.lisp
> *** - invalid byte #xFC in CHARSET:ASCII conversion

How do you dare feeding it invalid bytes?!?


> Or do you have any tip to avoid this ?

It's in the FAQ.


[···@thalassa tmp]$ echo "Fr�hkartoffel" | env -u LC_CTYPE ./foo.lisp
*** - invalid byte #xC3 in CHARSET:ASCII conversion

[···@thalassa tmp]$ echo "Fr�hkartoffel" | env LC_CTYPE=en_US.UTF-8 ./foo.lisp
70.114.252.104.107.97.114.116.111.102.102.101.108
[···@thalassa tmp]$ 


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
From: ············@gmail.com
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <1183477406.026819.161950@o61g2000hsh.googlegroups.com>
On Jul 3, 1:02 pm, Pascal Bourguignon <····@informatimago.com> wrote:
> ············@gmail.com writes:
> >> cat foo.lisp
> > #!/usr/local/bin/clisp
>
> > (format *terminal-io* "~{~A~^.~}~%" (map 'list #'char-code (read-line
> > *terminal-io* nil))))
>
> >> echo "Fr�hkartoffel" | foo.lisp
> > *** - invalid byte #xFC in CHARSET:ASCII conversion
>
> How do you dare feeding it invalid bytes?!?
>
> > Or do you have any tip to avoid this ?
>
> It's in the FAQ.
>
> [snip]

$ echo "Fr�hkartoffel" | env LC_CTYPE=en_US.UTF-8 foo.lisp
*** - invalid byte #xFC in CHARSET:ASCII conversion

It seems LC_CTYPE doesn't have any effect on clisp on my computer.

$ echo "Fr�hkartoffel" | env LC_ALL=en_US.UTF-8 foo.lisp
*** - invalid byte #xFC in CHARSET:UTF-8 conversion, not a Unicode-16

Furthermore, 'char code #x252 (displayed as '�' when using
iso-8859-1) ) is definitively not a valid UTF-8 character. I'm
wondering why this works on your computer.

Any pointer to this FAQ please ?

-Nicolas
From: Pascal Bourguignon
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <87y7hx8max.fsf@informatimago.com>
············@gmail.com writes:

> On Jul 3, 1:02 pm, Pascal Bourguignon <····@informatimago.com> wrote:
>> ············@gmail.com writes:
>> >> cat foo.lisp
>> > #!/usr/local/bin/clisp
>>
>> > (format *terminal-io* "~{~A~^.~}~%" (map 'list #'char-code (read-line
>> > *terminal-io* nil))))
>>
>> >> echo "Fr�hkartoffel" | foo.lisp
>> > *** - invalid byte #xFC in CHARSET:ASCII conversion
>>
>> How do you dare feeding it invalid bytes?!?
>>
>> > Or do you have any tip to avoid this ?
>>
>> It's in the FAQ.
>>
>> [snip]
>
> $ echo "Fr�hkartoffel" | env LC_CTYPE=en_US.UTF-8 foo.lisp
> *** - invalid byte #xFC in CHARSET:ASCII conversion
>
> It seems LC_CTYPE doesn't have any effect on clisp on my computer.
>
> $ echo "Fr�hkartoffel" | env LC_ALL=en_US.UTF-8 foo.lisp
> *** - invalid byte #xFC in CHARSET:UTF-8 conversion, not a Unicode-16
>
> Furthermore, 'char code #x252 (displayed as '�' when using
> iso-8859-1) ) is definitively not a valid UTF-8 character. I'm
> wondering why this works on your computer.

The point is that you should set as LC_ALL or LC_CTYPE encoding the
encoding you're using in your terminal!

If you use a ISO-8859-1 terminal, then set your LC_CTYPE / LC_ALL to
en_US.ISO-8859-1 !


> Any pointer to this FAQ please ?

http://clisp.cons.org/impnotes/faq.html
http://clisp.cons.org/impnotes/faq.html#faq-enc-err

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
From: Edi Weitz
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <uwsxin5zb.fsf@agharta.de>
On Mon, 02 Jul 2007 15:56:46 -0000, "Anton V. Belyaev" <·············@gmail.com> wrote:

> I encountered a simple task: to write a function, which takes a
> string and outputs ASCII codes of its characters, separated with
> dots.
>
> Example:
>
> Input: "ablahblah"
> Output: "97.108.97.98.108.97.98.108.97.107.112.111.120"

If that's the output generated by Python, I know why I'm not using
it... :)

> In Python it would be (not for holywars, just to make the task
> cleaner):
>
> ".".join([str(ord(c)) for c in "blablablakpox"])
>
> How do I write this in Common Lisp?

  (defun foo (string)
    (format nil "~{~A~^.~}" (map 'list #'char-code string)))

Edi.

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Thomas A. Russ
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <ymi1wfpqcf0.fsf@sevak.isi.edu>
Edi Weitz <········@agharta.de> writes:

> On Mon, 02 Jul 2007 15:56:46 -0000, "Anton V. Belyaev" <·············@gmail.com> wrote:
> 
> > I encountered a simple task: to write a function, which takes a
> > string and outputs ASCII codes of its characters, separated with
> > dots.
> 
>   (defun foo (string)
>     (format nil "~{~A~^.~}" (map 'list #'char-code string)))

Pedantic comment:

This might not solve the original specification, because CHAR-CODE in
Common Lisp is not at all guaranteed to give you ASCII code values.  At
the time of the standardization, the main competitor was EBCDIC.  Today,
one could perhaps get one of the Unicode codepoint representations
instead.

The only sure way to get ASCII would be to write your own ASCII-CODE
function to translate characters into their ASCII codes.  Unfortunately,
even that wouldn't be completely portable, because the required set of
supported characters in Common Lisp is only a subset of ASCII, so you
wouldn't necessarily be able to write all of ASCII.

But assuming that you have enough characters, including the control
characters, you could write this ASCII-CODE function as follows:

(defun ascii-code (char)
  (position char *ascii-string*))

where *ASCII-STRING* is a constant string with all of the characters in
order.

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Pascal Bourguignon
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <87myyd80tr.fsf@informatimago.com>
···@sevak.isi.edu (Thomas A. Russ) writes:

> Edi Weitz <········@agharta.de> writes:
>
>> On Mon, 02 Jul 2007 15:56:46 -0000, "Anton V. Belyaev" <·············@gmail.com> wrote:
>> 
>> > I encountered a simple task: to write a function, which takes a
>> > string and outputs ASCII codes of its characters, separated with
>> > dots.
>> 
>>   (defun foo (string)
>>     (format nil "~{~A~^.~}" (map 'list #'char-code string)))
>
> Pedantic comment:
>
> This might not solve the original specification, because CHAR-CODE in
> Common Lisp is not at all guaranteed to give you ASCII code values.  At
> the time of the standardization, the main competitor was EBCDIC.  Today,
> one could perhaps get one of the Unicode codepoint representations
> instead.
>
> The only sure way to get ASCII would be to write your own ASCII-CODE
> function to translate characters into their ASCII codes.  Unfortunately,
> even that wouldn't be completely portable, because the required set of
> supported characters in Common Lisp is only a subset of ASCII, so you
> wouldn't necessarily be able to write all of ASCII.

But happily, all the printable characters of ASCII are in the standard
lisp character set, so you'll be able to convert to ASCII codes all
the standard characters you can put in a lisp string.

Control codes are not characters, they're CODES, after all.


> But assuming that you have enough characters, including the control
> characters, you could write this ASCII-CODE function as follows:
>
> (defun ascii-code (char)
>   (position char *ascii-string*))
>
> where *ASCII-STRING* is a constant string with all of the characters in
> order.
>
> -- 
> Thomas A. Russ,  USC/Information Sciences Institute

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.
From: Edi Weitz
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <ubqesae14.fsf@agharta.de>
On Wed, 04 Jul 2007 02:33:20 +0200, Pascal Bourguignon <···@informatimago.com> wrote:

> Control codes are not characters, they're CODES, after all.

http://groups.google.com/group/comp.lang.lisp/msg/95a681795eac04cc

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Thomas A. Russ
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <ymiir8ypv0t.fsf@sevak.isi.edu>
Pascal Bourguignon <···@informatimago.com> writes:

> ···@sevak.isi.edu (Thomas A. Russ) writes:
> 
> > The only sure way to get ASCII would be to write your own ASCII-CODE
> > function to translate characters into their ASCII codes.  Unfortunately,
> > even that wouldn't be completely portable, because the required set of
> > supported characters in Common Lisp is only a subset of ASCII, so you
> > wouldn't necessarily be able to write all of ASCII.
> 
> But happily, all the printable characters of ASCII are in the standard
> lisp character set, so you'll be able to convert to ASCII codes all
> the standard characters you can put in a lisp string.
>
> Control codes are not characters, they're CODES, after all.

I suppose that depends on whether one considers Tab, for instance.  The
specification only requires that there be 94 non-blank standard
characters, #\Space and #\Newline.

Typically #\Return, #\Linefeed and #\Tab are of interest, and often
#\Newline is mapped to only one of the first two, or else to both of
them.


-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: ··········@hotmail.com
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <1183461924.580218.271580@w5g2000hsg.googlegroups.com>
just for fun(?)

(let ((a "abcdefghijklmnopqrstuvwxyz0123456789"))

(print (time
  (format nil "~{~d~^.~}" (map 'list #'char-code a))))

(print (time
  (coerce (remove nil
                  (loop for i across (format nil "~a"
                                             (loop for i across a
                                                   collect
                                                   (char-code i)))
                        collect
                          (cond
                            ((eq i #\Space) #\.)
                            ((eq i #\() nil)
                            ((eq i #\)) nil)
                            (t i))))
          'string)))

)
From: tichy
Subject: Re: Simple program: who can write a shorter code?
Date: 
Message-ID: <1183720867.066640.313780@g4g2000hsf.googlegroups.com>
On Jul 2, 5:56 pm, "Anton V. Belyaev" <·············@gmail.com> wrote:

> [ ... ]

> In Python it would be (not for holywars, just to make the task
> cleaner):
>
> ".".join([str(ord(c)) for c in "blablablakpox"])
>
> How do I write this in Common Lisp?

(defun foo (str)
  (collect-append 'string
                  (spread (catenate (make-series 0) (series 1))
                          (#M princ-to-string (#M char-code (scan
str)))
                          ".")))

(foo "blablablakpox")

==> "98.108.97.98.108.97.98.108.97.107.112.111.120"

Longer than FORMAT + MAP but possibly (?) faster.