f



Password input in lisp

Hello everyone,

I am new to this list and this is my first post so if I am going wrong some=
where, do let me know. :-)

So, how does one accept password input in common lisp(sbcl 1.0.57 on debian=
)? Like in gmail, where you type your password but just * appears in its pl=
ace. I found a function called read-passwd but thats in emacs lisp. For the=
 time being, security is not really a priority for me, since I need this fu=
nction as a part of an exercise.

Thanks
Aman 
0
amanj2718 (6)
11/16/2013 2:42:54 PM
comp.lang.lisp 16861 articles. 5 followers. Post Follow

15 Replies
493 Views

Similar Articles

[PageSpeed] 43

Aman Jotwani <amanj2718@gmail.com> writes:

> Hello everyone,
>
> I am new to this list and this is my first post so if I am going wrong
> somewhere, do let me know. :-)
>
> So, how does one accept password input in common lisp(sbcl 1.0.57 on
> debian)? Like in gmail, where you type your password but just *
> appears in its place. I found a function called read-passwd but thats
> in emacs lisp. For the time being, security is not really a priority
> for me, since I need this function as a part of an exercise.

You can't do that in Common Lisp.

Some languages are all encompassing.  I hear Visual Basic and Wolfram
Mathematica are like that, aggregating functions to do everything.  But
some better designed languages, notably programming languages, restrict
themselves to the description of programs, not to the aggregation of
words to designate all the possible actions in the universe.

On the other hand, practical programming languages provide means to use
libraries, so that you can add to the languages abstractions (functions
or other things) to expression those things that are not in the scope of
a programming language, but in the scope of some specific application.

Mind you, a lot of people already complain that Common Lisp is a big
standard with more than 1100 pages, and that Common Lisp implementations
are big programs at 30+ MB or generate big program (at the same 30+ MB),
when it already doesn't have networking, GUI, mobile computing, and yes,
it doesn't have password inputing.

Imagine the size it would have if it had everything!

Imagine the criticism we'd get when a hello world program would be 3 TB,
because it would include password inputing along with all the internet
protocols, all the possible and imaginable GUI widgets, etc.


I'm wondering why you're asking that about Common Lisp?  Is it the only
programming language you know?  Would you know languages like C, C++,
java, Python, etc?  Would you expect to find in those languages a way to
input passwords?  Without using a specific library providing a specific
user interface framework?


Perhaps you're considering terminal I/O, and then you might want to ask
how to use a library like termio to set the terminal in raw mode, or
perhaps you'd want to ask about how to use a library like ncurses?

Perhaps you're considering a GUI application, but using a unix-like
modular approach, and you're ready to use a tool dedicated to reading
passwords from the user, like ssh-askpass?  Then perhaps you'd want to
ask what libraries are available in lisp to let you run external
programs?

Or perhaps you're writing your GUI application with McCLIM and want to
know how to display stars when you receive key events? 


-- 
__Pascal Bourguignon__
http://www.informatimago.com/
0
pjb (7869)
11/16/2013 3:33:27 PM
Its not like I dont want to use an external library. I'd like to know one which can do the job. And I am just a student, already know C and Python, and only recently started learning and using Lisp and I really like it. 

So this password input problem was one that they gave in our college, while teaching Java. It's part of creating the hangman game (https://drive.google.com/file/d/1Ba-ZlpQ7k5ZHmQe83mqQLMCoBZSh8gXqEfStDgxM9zlRAyAefE1-8ApuP5Qd/edit?usp=sharing). 

So i thought I'll try to work it out in Lisp too as practice.

Aman
0
amanj2718 (6)
11/16/2013 4:10:18 PM
Aman Jotwani <amanj2718@gmail.com> writes:

> Its not like I dont want to use an external library. I'd like to know
> one which can do the job. And I am just a student, already know C and
> Python, and only recently started learning and using Lisp and I really
> like it.
>
> So this password input problem was one that they gave in our college,
> while teaching Java. It's part of creating the hangman game
> (https://drive.google.com/file/d/1Ba-ZlpQ7k5ZHmQe83mqQLMCoBZSh8gXqEfStDgxM9zlRAyAefE1-8ApuP5Qd/edit?usp=sharing).
>
> So i thought I'll try to work it out in Lisp too as practice.

You'd use the same external libraries than in C or Python, the ones I
mentionned.

But you've still not said what kind of user interface you wanted to
implement.


Assuming you want to do that in the terminal, you could use 
ncurses or just termios (termios is just in libc actually).

Since they're foreign to lisp, to use them you'd use CFFI, which is the
library that provides foreign function interface, (just like in Python).

And of course, wrapping those foreign functions in a lispy API is
already done and embedded in libraries such as cl-ncurses, or
iolib and terminfo.  They're available from quicklisp.

iolib.termios is on github, https://github.com/marsijanin/iolib.termios
you may clone it in ~/quicklisp/local-projects/


-- 
__Pascal Bourguignon__
http://www.informatimago.com/
0
pjb (7869)
11/16/2013 6:00:02 PM
What i've written so far is simply prompt and wait for input - http://paste.lisp.org/+3002 
(By the way, any suggestions wrt code style or maybe a more lispy way?)

I see you are suggesting a proper text interface using ncurses(which I have no experience with), etc. Though I didnt think about it before because its just an exercise, this user interface stuff is worth trying. Thanks a lot for these inputs.

Aman
0
amanj2718 (6)
11/16/2013 6:56:19 PM
On Saturday, November 16, 2013 3:42:54 PM UTC+1, Aman Jotwani wrote:
> So, how does one accept password input in common lisp(sbcl 1.0.57 on debian)? 

If using clisp is an option, you can use ext:with-keyboard (http://www.clisp.org/impnotes/terminal.html#with-kbd): 

(ext:with-keyboard
 (loop for char = (character (read-char ext:*keyboard-input*))
       when (alphanumericp char) collect char into password and do (princ "*")
       when (and (eql char #\Return) password) return (concatenate 'string password)))

Cheers,

Carlos
0
11/16/2013 6:59:08 PM
Aman Jotwani <amanj2718@gmail.com> writes:

> What i've written so far is simply prompt and wait for input - http://paste.lisp.org/+3002 
> (By the way, any suggestions wrt code style or maybe a more lispy way?)
>
> I see you are suggesting a proper text interface using ncurses(which I
> have no experience with), etc. Though I didnt think about it before
> because its just an exercise, this user interface stuff is worth
> trying. Thanks a lot for these inputs.

- Interactive I/O should be done thru *QUERY-IO* instead.

- I/O is most certainly buffered, therefore you should use FORCE-OUTPUT
  before reading.

- For the same reason, READ-CHAR (and even READ-CHAR-NO-HANG) may still
  require the user to enter a line.  Therefore it may not be what you
  want, and in any case, you should take into account that fact.

- It's not because you're writing a CLI program that you should forget
  all the good principles of user interface design and implementation.
  The MVC pattern is still perfectly applicable.  In any case, NEVER do
  I/O and computing in the same functions (unless that computing is
  directly related to the I/O, ie. you're computing a number of
  character to output, or a number of pixels to draw).

  The Model should be your business entities, here: Word, Letter,
  Hangman, Hangman-Game.

  The Views should be the functions that produce a representation of
  those entities for the user.

  The Controllers should be the functions that will manage user
  interaction and flow of control, taking input from the user and
  updating the model, and then calling up the views to display the new
  representations.



I see no password in a game of hangman.

1- why don't you choose a random word word /usr/share/dict/words ?

    (ql:quickload :com.informatimago.common-lisp.cesarum)

    ((lambda (sequence) (elt sequence (random (length sequence))))
      (load-time-value
       (remove-if-not
        (lambda (word)
         (every (function alpha-char-p) word))
        (com.informatimago.common-lisp.cesarum.file:string-list-text-file-contents
            "/usr/share/dict/words"))))

     --> "undemanding" ; for example


2- you can hide the user input conformingly, by writing a reasonably big
   number of newlines.  This should hide the echoed input beyond the
   visible window or paper area, and only a "cheater" would try to
   scroll back or thread the meters of paper flowing on the floor to try
   to see it.

     (defun hide-previous-input (&optional (stream *query-io*))
       (format stream "~200%"))


-- 
__Pascal Bourguignon__
http://www.informatimago.com/
0
pjb (7869)
11/17/2013 12:01:53 AM
"Pascal J. Bourguignon" <pjb@informatimago.com> writes:

> Aman Jotwani <amanj2718@gmail.com> writes:
>
>> What i've written so far is simply prompt and wait for input - http://paste.lisp.org/+3002 
>> (By the way, any suggestions wrt code style or maybe a more lispy way?)
>>
>> I see you are suggesting a proper text interface using ncurses(which I
>> have no experience with), etc. Though I didnt think about it before
>> because its just an exercise, this user interface stuff is worth
>> trying. Thanks a lot for these inputs.
>
> - Interactive I/O should be done thru *QUERY-IO* instead.
>
> - I/O is most certainly buffered, therefore you should use FORCE-OUTPUT
>   before reading.
>
> - For the same reason, READ-CHAR (and even READ-CHAR-NO-HANG) may still
>   require the user to enter a line.  Therefore it may not be what you
>   want, and in any case, you should take into account that fact.
>
> - It's not because you're writing a CLI program that you should forget
>   all the good principles of user interface design and implementation.
>   The MVC pattern is still perfectly applicable.  In any case, NEVER do
>   I/O and computing in the same functions (unless that computing is
>   directly related to the I/O, ie. you're computing a number of
>   character to output, or a number of pixels to draw).

And tests.

Here is an example: http://paste.lisp.org/display/139974

Notice that all the model part can be reused in a GUI application, or in
a web application.  Even the views could be reused in a web application,
with <pre></pre>.

So, exercise: quickload hunchentoot and write a controller to make
hangman a web application.


-- 
__Pascal Bourguignon__
http://www.informatimago.com/
0
pjb (7869)
11/17/2013 12:52:09 AM
Ah, clisp. But it's surprising that sbcl doesn't have something similar to offer.

Thanks a lot for the review, Pascal. That is one hell of an example :-) Really appreciate the help. As for the exercise...on it. :-)

Aman
0
amanj2718 (6)
11/17/2013 11:34:13 AM
Have to vocally concur on the example from Pascal---nice.

On Sunday, November 17, 2013 11:34:13 AM UTC, Aman Jotwani wrote:
> Ah, clisp. But it's surprising that sbcl doesn't have something similar to offer.
> 
> 
> 
> Thanks a lot for the review, Pascal. That is one hell of an example :-) Really appreciate the help. As for the exercise...on it. :-)
> 
> 
> 
> Aman

0
11/17/2013 12:37:52 PM
On 2013-11-16 09:42:54 EST, Aman Jotwani wrote:
> I am new to this list and this is my first post so if I am going wrong
> somewhere, do let me know. :-)
>
> So, how does one accept password input in common lisp(sbcl 1.0.57 on
> debian)? Like in gmail, where you type your password but just *
> appears in its place. I found a function called read-passwd but thats
> in emacs lisp. For the time being, security is not really a priority
> for me, since I need this function as a part of an exercise.

As others have responded there is no general solution for Common Lisp.
However, you might find a previous discussion interest, especially the
example code by Thomas F. Burdick for sbcl:

https://groups.google.com/forum/#!topic/comp.lang.lisp/CrmLfWNTx_U
-- 
Barry Fishman
0
11/17/2013 3:09:42 PM
On Saturday, November 16, 2013 9:42:54 AM UTC-5, Aman Jotwani wrote:
> So, how does one accept password input in common lisp(sbcl 1.0.57 on debi=
an)? Like in gmail, where you type your password but just * appears in its =
place. I found a function called read-passwd but thats in emacs lisp. For t=
he time being, security is not really a priority for me, since I need this =
function as a part of an exercise.

If you are using a language like Java or Visual X, you can easily code a gr=
aphical interface using the appropriate libraries. Common Lisp isn't like t=
his.

As far as I know, Common Lisp does not have easily accessible libraries for=
 doing this sort of thing, but that isn't why people use Lisp. People seem =
to use Lisp to solve really hard problems.

In my opinion, your best approach using Lisp, or Perl or Python or Ruby or =
a number of other languages, would be to build an HTML front end and use th=
e HTML widgets with an Common Lisp glue to your program. My primary working=
 language is Perl, which suffers from the same defect (lack of GUI toolkits=
), but Perl plays very nicely with Apache and SQL. You can also use Lisp fo=
r CGI-like web applications. It's easy and for a real application, you can =
use a client-server architecture rather than a stand-alone desktop architec=
ture.

CC.
0
cartercc (463)
11/17/2013 10:13:09 PM
This is my attempt at writing tests in Pascal's example code. Here it is -=
=20
http://paste.lisp.org/+301K.

There are some parts in the code which i don't understand and wasn't able t=
o get good answers on the net, they are - what exactly :redisplay does in l=
oop and why is :letter used in test/get-next-command. Where did :letter com=
e from? Is it some struct syntax to use struct variables easily? Also, what=
 purpose does #|nop|# serve?

Thanks Barry for the sbcl password hiding code, though I am nowhere close t=
o understanding it yet.

Anyways, the sheer power and flexibility of Lisp continues to amaze me. :-)

Aman
0
amanj2718 (6)
11/19/2013 5:20:50 PM
Aman Jotwani <amanj2718@gmail.com> writes:

> This is my attempt at writing tests in Pascal's example code. Here it is - 
> http://paste.lisp.org/+301K.
>
> There are some parts in the code which i don't understand and wasn't
> able to get good answers on the net, they are - what exactly
> :redisplay does in loop and why is :letter used in
> test/get-next-command. Where did :letter come from? 

Indeed, I'm sorry, I didn't put much docstrings or comments.  Here you
see the point of writing those things. ;-)

However, those can be understood by reading controller-play-game, or
indeed test/get-next-command.

It should have been:

(defun get-next-command (controller)
  "
DO:        Interact with the user to obtain the next command.
RETURN:    a list, either:
           (:letter character)  if the user entered a letter to try,
           (:redisplay)         if the hang man must be redisplayed,
           (:quit)              if the user wants to abort the game.
NOTE:      When there is invalid input get-next-command returns
           (:redisplay), expecting to be called again in the next
           iteration to ask the user again.
"
  (declare (ignore controller))
  (format *query-io* "Enter a letter, or QUIT: ")
  (finish-output *query-io*)
  (let ((input (string-trim *whitespaces* (read-line *query-io*))))
    (if (= 1 (length input))
        (if (alpha-char-p (aref input 0))
            (list :letter (aref input 0))
            (progn
              (format *query-io* "Please, type a letter, not '~A'. Try again.~%" input)
              '(:redisplay)))
        (if (string-equal "quit" input)
            '(:quit)
            (progn
              (format *query-io* "Please, type a letter or QUIT, not '~A'. Try again.~%" input)
              '(:redisplay))))))

(and similar docstrings could be provided for the other functions too.


> Is it some struct syntax to use struct variables easily? 

There's: 

    (with-accessors ((hangman       hangman-game-hangman) 
                     (word          hangman-game-word)
                     (guessed-count hangman-game-guessed-count)) game
       (do-something-with hangman word guessed-count)
       (incf guessed-count))

I hesitated to use it couple of times.  But as you can see, it's not
obviously a gain.

There's a few things you can do to improve the situation however.

1- you can provide a shorter 'conc-name':

    (defstruct (hangman-game
                  (:conc-name hg.))
      hangman
      word
      guessed-count)

   so now it's (hg.hangman game)
               (hg.word game)
               (hg.guessed-count game)

2- you could wrap defstruct in your own macro to keep track of the
   fields of the structure, and to provide additionnal features, like a
   macro specific to the structure to use easily its fields.  So you'd
   write:

    (define-structure hangman-game
      hangman
      word
      guessed-count)
                    
    (with-hangman-game (hangman word (gcount guessed-count)) game
       (do-something-with hangman word gcount)
       (incf gcount))


3- you could use CLOS objects instead of structures.
   Actually, there are a lot of reasons to use CLOS instead of
   structures:

    a- structures may not be redefined easily (it's implementation
       dependant); if you try to access with accessors created by a new
       version of the structure definition, objects created with a
       previous version of the structure definition you get undefined
       results (eg. crashes or other strange resultsl).
       This means that basically when you change structures, you need to
       reload all the code and recompute all the data using them.

    b- as you've seen in point 2, you can't get easily the name of slots
       of a structure, there's no standard structure introspection
       features.

    c- defstruct only provides a kind of single inheriting
       (cf. defstruct :include), while CLOS provides a very nice
       multiple-inheriting. 

    d- the accessors to the slots of the structure created by  defstruct
       are plain functions, not generic functions.  This is the reason
       why a strict adherance to the conc-name prefix to name those
       accessors is better enforced.  If two structures had a slot named
       GUESSED, you couldn't use GUESSED to name the accessors to both
       structures (unless the slot in the other structure was actually
       inherited from the first of course).  You could still write
       generic accessors yourself:
       
         (defgeneric guessed (object)
            (:method ((object structure-1)) (structure-1-guessed object))
            (:method ((object structure-2)) (structure-2-guessed object)))

         (defgeneric (setf guessed) (new-value object)
            (:method (new-value (object structure-1)) 
               (setf (structure-1-guessed object) new-value))
            (:method (new-value (object structure-2)) 
               (setf (structure-2-guessed object) new-value)))
       
       With CLOS, it's done automatically by DEFCLASS.

   So you could write:

      (defclass hangman-game () ; or (game) if there was a game superclass ;-)
        ((hangman :initarg :hangman :reader hangman)
         (word    :initarg :word    :reader word)   
         (count   :initform 0       :accessor guessed-count)))

   As you can see: 

      i- you can specificy whether you want just a reader (or a writer)
         or both a reader and writer (= accessor). You can also specify
         several different names for readers, writers and accessors.

      ii- you HAVE to specify a :initarg keyword if you want one.  If
          you don't specify it, then it'll be prevented to be
          initialized at instance creation. (Actually CLOS slots can be
          unbound cf. slot-boundp, while structure fields are always
          bound (default to NIL).

      iii- you can also specific a default value (initform), like in
           structures.
      
      iv- the name of the slot is internal (used in slot-boundp and
          slot-value), and can be different from the the name of the
          initarg and unrelated to the name of the accessors.

      v- however, what defclass doesn't provide is a nice readable
         syntax for CLOS objects.  They'll be printed by default as:
         #<HANGMAN-GAME #x1890590>  instead of
         #S(HANGMAN-GAME :HANGMAN #S(…) :WORD #S(…) :GUESSED-COUNT 3)
         and #< can't be read back while #S can.
         You have to provide a PRINT-OBJECT method and suitable readable
         syntax if you want this feature with CLOS objects.


> Also, what
> purpose does #|nop|# serve?

This is a comment. Like in pascal (* … *), CL #| … |# are nestable.
nop = No Operation = does nothing.
Here, it means that when the command is to :redisplay, we don't do
anything more, the redisplaying being done at the beginning of the next
iteration by (view-display (controller-game-view controller)).


> Thanks Barry for the sbcl password hiding code, though I am nowhere
> close to understanding it yet.
>
> Anyways, the sheer power and flexibility of Lisp continues to amaze
> me. :-)

-- 
__Pascal Bourguignon__
http://www.informatimago.com/
0
pjb (7869)
11/19/2013 11:50:15 PM
Ah, I got confused. All this time I thought :redisplay and :letter and :qui=
t were loop keywords or some standard functions, instead they are just symb=
ols which you return to decide the next course of action (no wonder they di=
dn't turn up in the HyperSpec ;-) ).

And yeah, accessing the slots was a pain with structs! CLOS is definitely a=
 better option. I am still shy to use macros though. I fail to make out whe=
n they are applicable and when they are not.

Aman
0
amanj2718 (6)
11/20/2013 5:40:33 AM
Aman Jotwani <amanj2718@gmail.com> writes:

> Ah, I got confused. All this time I thought :redisplay and :letter and
> :quit were loop keywords or some standard functions, instead they are
> just symbols which you return to decide the next course of action (no
> wonder they didn't turn up in the HyperSpec ;-) ).
>
> And yeah, accessing the slots was a pain with structs! CLOS is
> definitely a better option. I am still shy to use macros though. I
> fail to make out when they are applicable and when they are not.

Don't be shy.  Using, invoking or calling a macro is what you always do
in lisp.

There are three kinds of operators:

- special operators, which are: (block let* return-from catch
  load-time-value setq eval-when locally symbol-macrolet flet macrolet
  tagbody function multiple-value-call the go multiple-value-prog1 throw
  if progn unwind-protect labels progv let quote)

- macros (there are 66 macros defined in CL), and

- functions.

If you used only special operator, you'd not be programming in lisp, but
in assembler.  Well a high level assembler, since it still has lambda,
but notice how there's no setf.

It's the macros that makes lisp what it is.  SETF is a macro.  And
notice how the standard doesn't specify any way to put a key-value
association in a hash-table, but thru SETF.  Similarly for most "modern"
accessors, such as SYMBOL-PLIST; actually all the accessors that don't
access cons cells have no standard writer function provided. (And
neither those for cons cells, but at least you could write them as a
RPLACA/RPLACD of a reader; you couldn't do that for the other data
types).   If you didn't use macros, in CL, you couldn't even use most
of the data types!  Of course, there'd be no CLOS: there's no special
operator defined for CLOS, and almost no function are specific to CLOS.
CLOS is entirely defined as macros.

LOOP itself is a macro.

Anything interesting in lisp, and specific to lisp, is provided as a
macros.


Further, an implementation is allowed to implement special operators
themselves as macros!  So you could very well encounter a CL
implementation where there'd be no other thing than macros to use, apart
from the functions of course, but without the writer functions, you
couldn't do much.  Well, you could, just like in the 1960's, you could
implement a macro system and use it to define the more sophisticated
features we have in CL, again :-)  This is the Greenspun's Tenth Rule of
programming: it also applies to most non-CL lisps.



Now of course, as a beginner you may be wary of writing your own macros.
Actually, you should not either.  On the contrary, you should learn to
write macros, because it's easy, but sometimes a tad tricky, and because
as a newbie you need to learn when a macro is needed and useful and when
you should just use normal functions.  What you should be wary rather,
it's to have others use your newbie macros!  Including yourself ;-)

So, write macros, show them off, and be ready to be told either:

1- not to write a macro (use functions in those cases),

2- to use something else already existing in lisp or in libraries,

3- to write them differently,

4- or eventually, that they're ok, and you should document them and wrap
   them in a library that could be useful to somebody else.  but they
   you won't be a newbie since a few years already.


If you can write a function that builds a list, then you can write a
macro.

Suppose we have two entites:

(defstruct person name employers)
(defstruct company name employees)

With a n-n employers-employees relationship.

Now everytime we create an employers-employees association, we have to
modify two slots, and not forget to do so, so we could write a function
to do that:

  (defun add-employer-employee-association (c p)
     (push p (company-employees c))
     (push c (person-employers p)))

and similarly when we want to remove such a relation:

  (defun remove-employer-employee-association (p c)
    (setf (company-employees c) (delete p (company-employees c)))
    (setf (person-employers p)  (delete c (person-employers p))))


But a lot of functions similar to thoses will have to be written
everytime we have a relationship between two entities. 

Now, the question is, are you able to write a function that, given the
name of the entities, and the names of the end-points (the slots), would
generate those _lists_ !  yep, it's lists, code = data means that those
expressions above are data, mere lists, and that you should be able to
write functions to build such lists.



(defun scat (&rest string-designators)
  (intern (apply (function concatenate) 'string
                 (mapcar (function string) string-designators))))

(assert (eq (scat 'hello '- 'world) 'hello-world))
(assert (eq (scat 'hello '- "WORLD") 'hello-world))


(defun generate-add-relationship (left-entity left-end-point
                                  right-entity right-end-point)
  (list 'defun (scat 'add- left-end-point '- right-end-point '-association) '(left right)
     (list 'push 'left  (list (scat left-entity  '- left-end-point)  'right))
     (list 'push 'right (list (scat right-entity '- right-end-point) 'left))))


(generate-add-relationship 'company 'employees 'person 'employers)
--> (DEFUN ADD-EMPLOYEES-EMPLOYERS-ASSOCIATION (LEFT RIGHT) (PUSH LEFT (COMPANY-EMPLOYEES RIGHT)) (PUSH RIGHT (PERSON-EMPLOYERS LEFT)))

Now, to transform this data back into code, we could use EVAL:

(eval (generate-add-relationship 'company 'employees 'person 'employers))

But this would work only at run-time.  To tell the compiler that we want
to define a function, and so that it may compile it, we use macros: they
are just compiler hooks for our code generation functions.

(defmacro define-association (left-entity left-end-point
                              right-entity right-end-point)
   (list 'progn
      (generate-add-relationship left-entity left-end-point
                                 right-entity right-end-point)
      (generate-remove-relationship left-entity left-end-point
                                    right-entity right-end-point)
      #|…|#))

So now we can write:

(define-association company employees 
                    person employers)

and the compiler will automatically call the macro function that will
generate the (progn (defun add-employees-employers-association …)
                    (defun remove-employees-employers-association …))
list, so the compiler may take it as source code and compile it, thus
defining our two association functions.

(setf *print-circle* t)

(let ((p1 (make-person :name "John"))
      (p2 (make-person :name "Mark"))
      (c1 (make-company :name "Apple"))
      (c2 (make-company :name "Google")))
    (ADD-EMPLOYEES-EMPLOYERS-ASSOCIATION p1 c1)
    (ADD-EMPLOYEES-EMPLOYERS-ASSOCIATION p1 c2)
    (ADD-EMPLOYEES-EMPLOYERS-ASSOCIATION p2 c2)
   (list p1 p2 c1 c2))

--> (#2=#S(PERSON
           :NAME "John"
           :EMPLOYERS (#1=#S(COMPANY
                             :NAME "Google"
                             :EMPLOYEES (#3=#S(PERSON
                                               :NAME "Mark"
                                               :EMPLOYERS (#1#))
                                         #2#))
                       #4=#S(COMPANY
                             :NAME "Apple" 
                             :EMPLOYEES (#2#))))
          #3#
          #4#
          #1#)


PS: there's a reader macro in lisp that let you write more easily those
    lisp building functions: backquote.

Starting from:

  (defun add-employer-employee-association (c p)
     (push p (company-employees c))
     (push c (person-employers p)))

we prefix a backquote, and substitute each subexpressions that need to
be replaced by variables or expressions with a unsplice ,x or ,@x.

  `(defun ,(scat 'add- left-end-point '- right-end-point '-association) (c p)
     (push p (,(scat left-entity  '- left-end-point) c))
     (push c (,(scat right-entity '- right-end-point) p)))

Then we wrap the whole directly in the macro, since that is such a
simple expresion:


(defmacro define-association (left-entity left-end-point
                              right-entity right-end-point)
   `(progn

      (defun ,(scat 'add- left-end-point '- right-end-point '-association) (c p)
         (push p (,(scat left-entity  '- left-end-point) c))
         (push c (,(scat right-entity '- right-end-point) p)))

      (defun ,(scat 'add- left-end-point '- right-end-point '-association) (c p)
         (setf (,(scat left-entity  '- left-end-point) c) (delete p  (,(scat left-entity  '- left-end-point) c)))
         (setf (,(scat right-entity '- right-end-point) p) (delete c
         (,(scat right-entity '- right-end-point) p))))

      #|…|#))


But you could also just keep them in separate generating functions,
notably when the code to generate is more complex that this.

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


0
pjb (7869)
11/21/2013 10:32:45 PM
Reply:

Similar Artilces:

To LISP or not to LISP?
I know there have always been tons of posts on game AI boards asking about LISP, what it is, and if it's used in commercial games. Given that LISP is used in few commercial games, is it worth becoming fluent in anyway? I am considering persuing it for two reasons: A.) It is used in academic papers. Being fluent in LISP allows a person to stay on top of the latest developments in AI. B.) It can be embedded into game engines. I heard that Halo uses an embedded LISP-like language for AI. Could embedded LISP-style languages benificial in the long run? I've been reading through LISP by Winston & Horn in my free time for the past year, and I have to say that while I'm familiar with many of LISP's features, when it comes to coding in LISP, I have the speed and effectiveness of a slug. What's with this language? There are too many parentheses and stuff. I really like the transparency of lists in LISP, and have researched the benefits of functional programming, but I'm still not convinced that I can code anything in LISP with more ease than I could in C++. Should I persue LISP until I am fluent in it? Any advice? Kevin Clancy wrote: > I know there have always been tons of posts on game AI boards asking about > LISP, what it is, and if it's used in commercial games. > > Given that LISP is used in few commercial games, is it worth becoming fluent > in anyway? > > I am considering persuing it ...

why Common Lisp is not a lisp
How about, when trying to decide "what is lisp" or to apply "lispp" to some _thing_, we just go very low level and somewhat retro and mostly informal? As a straw-man: a lisp is any language implementation which includes a run time system with some recognizably lispish types (cons pairs, numbers, symbols) having their familiar properties (latent typing, gc-based memory management), and implemented in a recognizably traditional way (or a brilliant new way, worthy of being added to the design space). In the language implemented, programs are expressed in s-exp syntax and th...

Which lisp for a (lisp) novice?
I'd like to learn lisp but I'm confused with the many variants I'm finding. What would you recommend for an experienced programmer in other languages (mainly C and perl), wanting to learn lisp? Should be a well documented lisp and working in Mac OS and main UNIXes (Solaris, HP-UX, AIX, ...) I've found and enjoyed the "Practical Common Lisp" book, mainly because it starts with realistic examples, not the car/cdr/lists I had found in the past that lead me to the wrong conclusion that lisp was "only" good at processing abstract lists and could do nothin...

I wish there was a livejournal in lisp. A meetup.com a lisp craigslist. A lisp drudgereport. A lisp forum like this. so I had more things to point at and say hey that is lisp powered.. may
I wish there was a livejournal in lisp. A meetup.com a lisp craigslist. A lisp drudgereport. A lisp forum like this. so I had more things to point at and say hey that is lisp powered.. maybe a lisp cpan with vote up your favorite project in various categories. ...

lisp web servers, lisp as a shell, lisp using rdbms or not
1 I saw the webserver huchentoot seems to want to sit behind apache? araneida seesm to also? Why do they depend on apache? 2 I saw lisp on lines is kinda alpha 3 uncomon web seems cool but also wants to sit behind apache 4 common lisp as a shell is really doible? anyone do this? 5 for large apps is using a rdbms unavoidable? 6 does lisp support or mapping or persisten db conneciton pooling and cacheing? gavino wrote: > 1 I saw the webserver huchentoot seems to want to sit behind apache? > araneida seesm to also? Why do they depend on apache? I can't comment on those particula...

#lisp macros and lisp fanatics
lisp macros and lisp fanatics. [original g+ post https://plus.google.com/b/116187821854023674058/116187821= 854023674058/posts/NewjjhGz4js ] as you may know, i think lisp macro is one of the most idiotic thing in computer science. (and cons, and lisp's IRREGULAR nested syntax) i've wrote perhaps 10 articles on this, and been arguing with lispers for about 10 years. still, once in a while, out of the blue (or, as Naggum says: crawling out of the woodwork), i got some comment or email, criticizing my criticism on lisp. the degree of quality, and what i can guess of the person's ...

Lisp Metadata Importer in Lisp?
Hi, Does a similar software to John Wiseman's "Lisp Metadata Importer" (http://lemonodor.com/archives/001232.html), written in Lisp, exists? The functionality i'm looking for is: extract from Lisp source code an indexed list of symbols and definitions with individual properties. I am currently in the process of writing one, but i have some problems with (read) and packages and other syntactic sugars (macro- characters). Adrian D. ...

Why Common Lisp is not a Lisp, continued
[I am picking up this thread again that came to a rest some time ago. I am doing this because it took me relatively long to dig out an article referenced in the posting below.] [I know that such topics are likely to ignite heated discussions between Common Lispniks and Schemers, and some find them practically irrelevant. But, heck, I am interested in these things, and I don't know where else to discuss them. ;) ] > From: Rainer Joswig <joswig@lispmachine.de> > Subject: Re: why Common Lisp is not a lisp > Date: Wed, 03 Sep 2003 20:01:48 +0200 > Message-ID: <joswi...

On Lisp: the 6 keystones of Lisp
Trying to get a handle on CLOS, I grabbed my Kinkos copy of "On Lisp" and jumped far ahead to the CLOS chapter. In section 25.1, there is a footnote (which in the .pdf version is for page 349) which reads: "Efforts to sum up Lisp in a single phrase are probably doomed to failure, because the power of Lisp arises from the combination of at least five or six features." OK, I can think of 1 extensibility 2 garbage collection 3 functions as a type 4 ... oh-oh out of newbie guesses -- Everyman has three hearts; one to show the world, one to show friends, and one onl...

A tiny Lisp written in Lisp
Hi ! It exists a page that defines a tiny Lisp in Lisp very tersely. I'm pretty sure to have seen it on the list about one year ago. However I'm not able to find it again. I wonder if Pascal Bourguignon exhibited this program, not absolutely sure to remember. Please have you got any idea about this small program ? Thanks. Fabrice Fab <fabricemarchant@free.fr> writes: > Hi ! > > It exists a page that defines a tiny Lisp in Lisp very tersely. > > I'm pretty sure to have seen it on the list about one year ago. > > However I'm not...

Lisp vs. Common Lisp
I'm deeply interested in Lisp. I'm learning it through Peter Seibel's PCL. One comment I find a lot when people talk about Lisp is that Lisp is wonderful, but CL has a lot of problems (problems I cannot understand due to my newbieness). Now, I don't want to learn the wrong Lisp. What do people mean by Lisp in this context? Is it Lisp 1, Lisp 1.5, the particular dialect they used and fall in love with? If this unidentified Lisp is so great, why don't they use it and avoid all the pitfalls of CL? On Sat, 12 Jun 2010 09:36:50 -0700, Simon wrote: > I'm...

lisp
can somebody help me : I want to implement all the common lisp functions in C++. Thanks Sagar X-No-archive: yes "Sagar" <sagjo@hotmail.com> wrote in message news:4ffee5b5.0311051924.42b7de13@posting.google.com... > can somebody help me : > I want to implement all the common lisp functions in C++. Why would you want to do that? I'm sure many elementary Lisp and Scheme interpreters in C are available on the internet. I assume that you mean "common functions in lisp" and not "Common Lisp functions." M. sagjo@hotmail.com (Sagar) wrote: > can ...

Lisp
I'm thinking of doing some of the high-level logic in my program in lisp. Is there an easy way to integrate lisp with C? At the moment my thoughts are moving along the lines of a cheap and cheerful embedded lisp interpreter, written in C. However I am also feeling that it shouldn't be necessary to do this. In article <YM6dndbD_MPVDgLYnZ2dnUVZ8s-qnZ2d@bt.com>, Malcolm McLean <regniztar@btinternet.com> wrote: >I'm thinking of doing some of the high-level logic in my program in >lisp. > >Is there an easy way to integrate lisp with C? At the moment my >...

lisp
I need a lisp to draw and unfold cone , rectangle to round transition and rectangle to rectangle transition thks Have a look at www.black-cad.de. There is offered a general unfolding program for AutoCAD. Furtehrmore I wrote a routine to create any solid(!) transitions - even from rectangle to round. Regards Jochen "info" <eric@serveur.pdcamb> schrieb im Newsbeitrag news:carh88$1191$1@biggoron.nerim.net... > I need a lisp to draw and unfold cone , rectangle to round transition and > rectangle to rectangle transition > thks > > nothing free ? "jochen" <scj.schulz@t-online.de> a �crit dans le message de news: carmrg$p08$06$1@news.t-online.com... > Have a look at www.black-cad.de. > There is offered a general unfolding program for AutoCAD. Furtehrmore I > wrote a routine to create any solid(!) transitions - even from rectangle to > round. > Regards > Jochen > > "info" <eric@serveur.pdcamb> schrieb im Newsbeitrag > news:carh88$1191$1@biggoron.nerim.net... > > I need a lisp to draw and unfold cone , rectangle to round transition and > > rectangle to rectangle transition > > thks > > > > > > how much free stuff have you contributed?? "info" <eric@serveur.pdcamb> wrote in message news:carnst$14pg$1@biggoron.nerim.net... > nothing free ? > "jochen" <scj.schulz@t-on...

Lisp
I would like to know if Lisp provides List Comprehensions (Zermelo Frankel). Also I am interested in logic programming. Besides LispWorks which provides an embedded prolog is there either a production rule system (I checked out LISA but have not yet installed) or prolog module available for lisp? I did look at Schelog for scheme but would like to know if such is available for lisp. I have downloaded on my Mac OSX SBCL, Clisp, and OpenMCL does anyone have any preference to any of these, or other recommendations? I have PAIP, both of grahams books, winston's book, the steele reference, etc...

lisp
Hi Where do I put downloaded elisp files for them to work? -do I need to run a script or restart emacs afterwards? I run aquamacs on OSXsnow. /Peter Hans-Peter Engelund Kristiansen <hpek@phys.au.dk> writes: > Hi > > Where do I put downloaded elisp files for them to work? -do I need to > run a script or restart emacs afterwards? > I run aquamacs on OSXsnow. > > /Peter I don't use aquamacs anymore, but it respects .emacs IIRC. So if you add a load path to your .emacs it should correctly work as it would in any other variant. You may want to check out the bu...

My road to Lisp (or why Lisp for poker software)
I was asked in a different thread to elaborate on why I chose Lisp to write poker room software, whether I had to convince my backers, etc. Poker room software, if you are wondering, is the client that you download at, say, http://www.pokeroom.com and the server that the client connects to. The short answer is http://userpages.umbc.edu/~bcorfm1/C++-vs-Lisp.html and no, because I have no backers. The longer answer is quite long because I'm driven not only by business rationale but also by a bunch of emotional reasons. I started programming at about 14 while in Cuba and using Basic. I thi...

Emacs Lisp & Common Lisp
A question: If I try to learn Emacs Lisp while also learning Common Lisp, am I going to damage my understanding of either in some way? "Paul Lange" <plange@gmail.com> writes: > A question: > > If I try to learn Emacs Lisp while also learning Common Lisp, am I > going to damage my understanding of either in some way? Depends whether you can keep them separate in your mind. If you can, observing the differences should be educational. But if not, you'll possibly waste some time discovering the differences the hard way, namely having your programs not work in o...

Apprendre Lisp (or NOT!) [Weird Lisp History]
Weird Lisp History: In ~1980 a friend of mine and I, in our senior year at Penn, wrote a Lisp book call "Learning Lisp". This was in the time of the Apple II, WAY before CL, and I think before most of the popular lisp textbooks of the time, like Toureztky's [I'm not too sure of all the publication dates of all those, but I think that if they had existed, we wouldn't have felt the need to write one of our own!] We never published it... But wait, this gets more interesting! At about the same time, a mutual friend of ours independently wrote a toy Apple II Lisp interpreter...

How much of a lisp compiler is written in lisp ?
Hello, I would like to have an idea of how much of the lisp compilers you know are written in lisp and how much is written in something else. A similar but probably a bit different question is: how much of the lisp compilers you know involves lisp-level program transformation, and how much actually involves traduction to machine code ? Thanks -- 5th European Lisp Workshop at ECOOP 2008, July 7: http://elw.bknr.net/2008/ Didier Verna, didier@lrde.epita.fr, http://www.lrde.epita.fr/~didier EPITA / LRDE, 14-16 rue Voltaire Tel.+33 (0)1 44 08 01 85 94276 Le Kremlin-Bic�tre, France...

lisp newbie
Hi all, I am in a programming languages class, and Lisp has me freaked out! I have a basic programming assignment where we make some functions and then make a function EVALUATE that takes in a combination of those functions as a list from a .data text file, and evaluates it. Here's what is tearing me up: how do I program here? I am used to using a text editor (pico) in our Unix system, and making a .c or .cpp or .f or .p file, and then compiling it, and then running it, perhaps with file or command line i/o. I can't figure out how to get this program going! I have my functions written, but I can't even test them out because I don't know even know it all is supposed to be run. In gcl, our Lisp environment, you enter it by typing "gcl", and then exit it by (bye). You can type Lisp expressions at the prompt while gcl. I am totally confused. Can anyone help me get oriented, or is there a link? Our professor is not that available to a working student, even by email. Thanks, Dan Neill djneill@iwebworks.com wrote: > Hi all, > I am in a programming languages class, and Lisp has me freaked out! I > have a basic programming assignment where we make some functions and > then make a function EVALUATE that takes in a combination of those > functions as a list from a .data text file, and evaluates it. > > Here's what is tearing me up: how do I program here? I am used to > using a text editor (pico) in our Unix system,...

Lisp libraries
Hi, I had an idea earlier, after reading the various threads and articles on Reddit's switch to Python. The problem, people say, is that Lisp has a library deficiency, so why not call Python's library code from Lisp? A while ago, I needed to use Python as an embedded scripting language, so I found out how to do this. Shelling out to Python was not an option because it's grossly inefficient (and not Windows-friendly), but Python is actually embeddable, in a fashion where it is called from a DLL, and remains resident in memory until Lisp wants to execute Python code (as text)...

Cross-lisp questions and my #lisp experience
Summary: I have recently tried to inquire about Common Lisp web frameworks vs Scheme web frameworks on #lisp (I know, it smells like a flamebait, but I really didn't mean that). This post is a summary of my experiences, as well as a restatement of my questions. I originally learned Lisp at age 10 (about 20 years ago), used it in college (about 10 years ago) ant only returned to it about a year ago due to Paul Graham's recent advocacy. Naturally, I figured that the best hacker real estate is in web programming because this is the only way to escape the long shadow of C/C++/Java. I sett...

Lisp-1 and Lisp-2 in harmony.
Hey all, In TXR's built-in Lisp dialect, TXR Lisp, which is a Lisp-2, I implemented a way of doing Lisp-1 semantics. The [...] notation is a shorhand for (dwim ...). Dwim stands for "Dispatch, in a way that is intelligent and meaningful". The arguments to dwim are evaluated according to Lisp-1 rules. Any argument which is a symbol is resolved using the innermost binding which is either in the variable or function namespace. If the innermost scope which has a binding for the symbol has a binding in both namespaces, the tie is resolved in favor of the variable binding. (I thou...

Web resources about - Password input in lisp - comp.lang.lisp

Password strength - Wikipedia, the free encyclopedia
Password strength is a measure of the effectiveness of a password in resisting guessing and brute-force attacks. In its usual form, it estimates ...

California Employers, College Administrators Can’t Ask For Facebook Passwords
As the link between Facebook usage and work comes under closer scrutiny, several states have made it a law that employers cannot request social ...

Protecting Your Passwords and Your Privacy
Facebook and Privacy hat eine Notiz mit dem Titel Protecting Your Passwords and Your Privacy geschrieben. Du kannst den vollständigen Text hier ...

New Facebook Security Features: One-Time Passwords, Security Info
Facebook has launched two new security features to help users stay in control of their accounts. Users can now receive a one-time password from ...

Here are the passwords cybercriminals are most likely to use in hacking attempts
The security group Rapid7 has released a new report that may prompt some alarm from web users who are heedless when it comes to choosing their ...

Mastercard's app that replaces passwords with selfies is coming this year
Everybody hates passwords. And Mastercard may have cracked the code on replacing it — with selfies. Mastercard announced at the Mobile World ...

Review: 8 password managers for Windows, Mac OS X, iOS, and Android
LastPass, 1Password, and Dashlane edge KeePass and company in features, flexibility, and ease

MasterCard to replace passwords with selfies
MasterCard customers will soon be able to replace their passwords with a “selfie” and a fingerprints to verify their identity.

FBI rebuts criticism that it reset terrorist's iCloud password after attack
Computerworld FBI rebuts criticism that it reset terrorist's iCloud password after attack Computerworld The Federal Bureau of Investigation ...

Ultimate guide to iPhone and iPad password manager apps
As we store more and more sensitive info on our iPhones and iPads, password manager apps are more important than ever. Here's why!

Resources last updated: 3/7/2016 12:23:18 PM