From: Steve Ellis
Subject: a few newbie questions
Date: 
Message-ID: <8uVHb.3116$r01.2084@news-binary.blueyonder.co.uk>
Hi everyone

the first problem is that i've created a function which outputs a generated
list as a table in the following format :

Table Header
---------------

item      ||     item
item      ||     item
item      ||     item
biggeritem   ||     item

as you can see, this doesn't work very well since it doesn't line up
properly. Is there a way to make it line up ?? The only way i can think of
is to vary the number of spaces based on how long the word in the list is
but i don't know of a function to return the length of a string.

The second question i have is does anyone know where i can find a sorting
algorithm since i'm not sure how to go about doing one in LISP

Sorry if i haven't explained them very well but hopefully you'll know what i
mean (particularly with the first one)

thanks for your time

Steve

From: Howard Ding <······@hading.dnsalias.com>
Subject: Re: a few newbie questions
Date: 
Message-ID: <m3n09bkcis.fsf@frisell.localdomain>
"Steve Ellis" <··········@blueyonder.co.uk> writes:

>
> as you can see, this doesn't work very well since it doesn't line up
> properly. Is there a way to make it line up ?? The only way i can think of
> is to vary the number of spaces based on how long the word in the list is
> but i don't know of a function to return the length of a string.

How about "length"?

>
> The second question i have is does anyone know where i can find a sorting
> algorithm since i'm not sure how to go about doing one in LISP
>

How about using the built in function "sort"?

-- 
Howard Ding
<······@hading.dnsalias.com>
From: Björn Lindberg
Subject: Re: a few newbie questions
Date: 
Message-ID: <hcsfzf3bwcv.fsf@knatte.nada.kth.se>
"Steve Ellis" <··········@blueyonder.co.uk> writes:

> Hi everyone
> 
> the first problem is that i've created a function which outputs a generated
> list as a table in the following format :
> 
> Table Header
> ---------------
> 
> item      ||     item
> item      ||     item
> item      ||     item
> biggeritem   ||     item
> 
> as you can see, this doesn't work very well since it doesn't line up
> properly. Is there a way to make it line up ?? The only way i can think of
> is to vary the number of spaces based on how long the word in the list is
> but i don't know of a function to return the length of a string.

The function LENGTH gives the length of any sequence, inclduign strings.

> The second question i have is does anyone know where i can find a sorting
> algorithm since i'm not sure how to go about doing one in LISP

Have a look at SORT:

* (sort '(("abc" . 3) ("ghi" . 2) ("def" . 4)) #'string-lessp :key #'first)
(("abc" . 3) ("def" . 4) ("ghi" . 2))

* (sort '(("abc" . 3) ("ghi" . 2) ("def" . 4)) #'< :key #'cdr)
(("ghi" . 2) ("abc" . 3) ("def" . 4))


Bj�rn
From: Edi Weitz
Subject: Re: a few newbie questions
Date: 
Message-ID: <87u13jsqh8.fsf@bird.agharta.de>
On 29 Dec 2003 14:13:20 +0100, ·······@nada.kth.se (Bj�rn Lindberg) wrote:

> "Steve Ellis" <··········@blueyonder.co.uk> writes:
>
>> Hi everyone
>> 
>> the first problem is that i've created a function which outputs a
>> generated list as a table in the following format :
>> 
>> Table Header
>> ---------------
>> 
>> item      ||     item
>> item      ||     item
>> item      ||     item
>> biggeritem   ||     item
>> 
>> as you can see, this doesn't work very well since it doesn't line
>> up properly. Is there a way to make it line up ?? The only way i
>> can think of is to vary the number of spaces based on how long the
>> word in the list is but i don't know of a function to return the
>> length of a string.
>
> The function LENGTH gives the length of any sequence, inclduign
> strings.

The OP might also want to look at the FORMAT function:

  <http://www.lispworks.com/reference/HyperSpec/Body/22_c.htm>

* (loop for (a b) in '((item item) (item item) (biggeritem item))
        do (format t ··@20<~A~> || ·@20<~A~>~%" a b))
ITEM                 || ITEM
ITEM                 || ITEM
BIGGERITEM           || ITEM
NIL

>> The second question i have is does anyone know where i can find a
>> sorting algorithm since i'm not sure how to go about doing one in
>> LISP
>
> Have a look at SORT:
>
> * (sort '(("abc" . 3) ("ghi" . 2) ("def" . 4)) #'string-lessp :key #'first)
> (("abc" . 3) ("def" . 4) ("ghi" . 2))
>
> * (sort '(("abc" . 3) ("ghi" . 2) ("def" . 4)) #'< :key #'cdr)
> (("ghi" . 2) ("abc" . 3) ("def" . 4))

To the OP: The usual warnings apply here. First, SORT is a
/destructive/ function, i.e. it modifies the sequence to be
sorted. But that doesn't mean that it is to be used like this:

* (sort my-seq #'<)
;; MY-SEQ is now sorted

The correct idiom is:

* (setq my-seq (sort my-seq #'<))

Second, the example by Bj�rn modifies literal constants. This is OK to
do in the REPL but you should never do this in a file with code to be
loaded/compiled. See

  <http://www.lispworks.com/reference/HyperSpec/Body/03_ga.htm>

Edi.
From: Björn Lindberg
Subject: Re: a few newbie questions
Date: 
Message-ID: <hcsbrprbunn.fsf@knatte.nada.kth.se>
Edi Weitz <···@agharta.de> writes:

> >> The second question i have is does anyone know where i can find a
> >> sorting algorithm since i'm not sure how to go about doing one in
> >> LISP
> >
> > Have a look at SORT:
> >
> > * (sort '(("abc" . 3) ("ghi" . 2) ("def" . 4)) #'string-lessp :key #'first)
> > (("abc" . 3) ("def" . 4) ("ghi" . 2))
> >
> > * (sort '(("abc" . 3) ("ghi" . 2) ("def" . 4)) #'< :key #'cdr)
> > (("ghi" . 2) ("abc" . 3) ("def" . 4))
> 
> To the OP: The usual warnings apply here. First, SORT is a
> /destructive/ function, i.e. it modifies the sequence to be
> sorted. But that doesn't mean that it is to be used like this:
> 
> * (sort my-seq #'<)
> ;; MY-SEQ is now sorted
> 
> The correct idiom is:
> 
> * (setq my-seq (sort my-seq #'<))

Thanks for the correction. I was just quickly typing up an example at
the REPL and I messed up.


Bj�rn
From: Steve Ellis
Subject: Re: a few newbie questions
Date: 
Message-ID: <0k0Ib.661$sK2.421@news-binary.blueyonder.co.uk>
Hi

thanks for the help guys, hopefully i'll be able to get everything done now

Steve

"Steve Ellis" <··········@blueyonder.co.uk> wrote in message
························@news-binary.blueyonder.co.uk...
> Hi everyone
>
> the first problem is that i've created a function which outputs a
generated
> list as a table in the following format :
>
> Table Header
> ---------------
>
> item      ||     item
> item      ||     item
> item      ||     item
> biggeritem   ||     item
>
> as you can see, this doesn't work very well since it doesn't line up
> properly. Is there a way to make it line up ?? The only way i can think of
> is to vary the number of spaces based on how long the word in the list is
> but i don't know of a function to return the length of a string.
>
> The second question i have is does anyone know where i can find a sorting
> algorithm since i'm not sure how to go about doing one in LISP
>
> Sorry if i haven't explained them very well but hopefully you'll know what
i
> mean (particularly with the first one)
>
> thanks for your time
>
> Steve
>
>
>
From: Kalle Olavi Niemitalo
Subject: Re: a few newbie questions
Date: 
Message-ID: <87smiztswh.fsf@Astalo.kon.iki.fi>
"Steve Ellis" <··········@blueyonder.co.uk> writes:

> Table Header
> ---------------
>
> item      ||     item
> item      ||     item
> item      ||     item
> biggeritem   ||     item

I wonder if this can be done with FORMAT.

  (let ((data '(("item" "item")
                ("item" "item")
                ("item" "item")
                ("biggeritem" "item"))))
    (let ((leftmost-column-width (loop for (item) in data
                                       maximize (length item))))
      (format t "~&~:{~8A||~A~%~}" data)))

How can I use the value of leftmost-column-width as the parameter
in "~8A"?  I don't think I can use "~VA", because that would
attempt to read the width from within the data list.

I could compute the format string with another FORMAT, but then
I'd have to double all the tildes, and such a hack would also
thwart any FORMATTER-like optimizations.

A loop around the FORMAT call would do the job, but I'm not yet
fluent with FORMAT, so I wonder if there's some trick I missed.