Re: Beginner - Function Critique
- From: Alan Crowe <alan@xxxxxxxxxxxxxxxxxxxxxxx>
- Date: 30 Sep 2005 19:33:45 +0100
crystal1 wrote
> These functions have been created a thousand times and are
> trivial, but I wrote these instances myself and am a
> pretty proud beginner. What do you think?
I think you are doing great. With such spirit and playfulness
your explorations will soon discover all of Common Lisp
I particularly liked the idea of using getf to extract a named
return value from amongst several. Other posters have shown
you the right way to return multiple values with values and
multiple-value-thing but in a spirit of play have a look at
this kitten-code. The idea is that
(:string "foo" :remainder "bar")
looks like a keyword argument list, so let us define a
function with keyword arguments and apply it to the list.
Then the keyword processing is doing the getf's for us.
(defun line-split (s d)
(if (= (length s) 0)
'()
(apply #'construct d (helper s d))))
(defun construct (d &key string remainder)
(cons string
(line-split remainder d)))
(defun helper (s d)
(let ((split (position d s)))
(if split
(list :string (subseq s 0 split)
:remainder (subseq s (+ split 1)))
(list :string s))))
(line-split "will florence ever get to kiss winston? " #\space)
=> ("will" "florence" "ever" "get" "to" "kiss" "winston?")
This code only works because keyword arguments default to
nil, and nil is a list of length zero, so
(= (length nil) 0) => t
The proper thing to do is to give remainder an explict
default of the string with no characters
(defun construct (d &key string (remainder "")) ... )
Then your excellent check-type's will be satisfied.
Alan Crowe
Edinburgh
Scotland
.
- References:
- Beginner - Function Critique
- From: crystal1
- Beginner - Function Critique
- Prev by Date: Re: C++ and lisp and Games
- Next by Date: The limits of lisps synatactic gymnastics? (i sure hope not)
- Previous by thread: Re: Beginner - Function Critique
- Next by thread: macro question
- Index(es):