Author Topic: getting initals from a sting in lisp  (Read 2318 times)

0 Members and 1 Guest are viewing this topic.

marcoblanco120

  • Guest
getting initals from a sting in lisp
« on: August 18, 2011, 03:43:28 PM »
Can anyone provide a function that will pull the first letter only from each word in a multi-word text string? 
i.e. "My name is Bill" returns "MNIB"

Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: getting initals from a sting in lisp
« Reply #1 on: August 18, 2011, 03:47:33 PM »
Quick 'n dirty:

Code: [Select]
(defun _Initials ( s )
    (vl-list->string
        (apply 'append
            (mapcar
               '(lambda ( a b ) (if (and (= 32 a) (/= 32 b)) (list b)))
                (cons 32 (vl-string->list s))
                (vl-string->list (strcase s))
            )
        )
    )
)

Code: [Select]
_$ (_Initials "My name is Bill")
"MNIB"

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: getting initals from a sting in lisp
« Reply #2 on: August 18, 2011, 10:51:56 PM »
Obfuscated fun in case Se7en is bored ...

Code: [Select]
(defun _Initials ( text )
    ((lambda (a b c) ((lambda (d) (a (mapcar 'b (cons 32 d) d))) (c text)))
     (lambda (a) (vl-list->string (vl-remove-if 'null a)))
     (lambda (a b) (if (and (= 32 a) (/= 32 b)) b))
     (lambda (a) (vl-string->list (vl-string-trim " \n\t" (strcase a)))))
)
« Last Edit: August 18, 2011, 10:56:48 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10653
Re: getting initals from a sting in lisp
« Reply #3 on: August 18, 2011, 11:34:35 PM »
heh. I cant read lisp anymore--? like in the old days ?--so it's all obfuscated to me now. *lol*
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10653
Re: getting initals from a sting in lisp
« Reply #4 on: August 19, 2011, 11:44:56 AM »
I thought it would be fun to construct a mini parser for generating acronyms while I was waiting for one of my coworkers. So using my other parser "studies" as a basis, I generated this (However, I only spent 15 minutes on this so beware):

Code: [Select]
enum Token
{
    tok_eof = -1,
    // end of file
    tok_identifier = -2
};

static std::string IdentifierString;
static int CurTok;
// oops; globals

static int gettok()
{
    static int LastChar = ' ';
    //   skip whitespace
    while (isspace(LastChar))
        LastChar = getchar();

    if (isalpha(LastChar)) {
        IdentifierString = LastChar;
        while (isalpha(LastChar))
            LastChar = getchar();
        return tok_identifier;
    }

    // Check for end of file.  Don't eat the EOF.
    if (LastChar == EOF)
        return tok_eof;

    // Otherwise, just return the character as its ascii value.
    int ThisChar = LastChar;
    LastChar = getchar();
    return ThisChar;
}

static int getNextToken()
{
    return CurTok = gettok();
}

static void HandleIdentifier()
{
    std::cout << IdentifierString;
    getNextToken();
    // prime the next token
    return;
}

static void MainLoop()
{
    while (1)
    {
        switch (CurTok)
        {
        case tok_eof:           return;
        case tok_identifier:    HandleIdentifier(); break;
        default:                getNextToken(); break;
        }
    }
}

int main(int argc, char *argv[])
{
    std::cout << "\nPress \"CTRL-Z\" when finished.\n";
    MainLoop();
    return 0;
}

Of course this is grossly over doing it but I wanted this to represent a mini model of my LispParser trial (it not really an accurate model, but it's kind of in the same spirit).

Now, watch this. We can eliminate quite a bit of code by making two minor changes to the gettok() function.

Code: [Select]
static int gettok()
{
    static int LastChar = ' ';
    //   skip whitespace
    while (isspace(LastChar))
        LastChar = getchar();

    if (isalpha(LastChar))
    {
        IdentifierString += LastChar;   // changed
        while (isalpha(LastChar))
            LastChar = getchar();
        return gettok();                // changed
    }

    // Check for end of file.  Don't eat the EOF.
    if (LastChar == EOF)
        return 0;
}

int main(int argc, char *argv[])
{
    std::cout << "\nPress \"CTRL-Z\" when finished.\n";
    gettok();
    std::cout << IdentifierString << '\n';
    return 1;
}

Same functionality (almost).

With the first example you can generate a GIANT acronym from a file full of text like:
[COMPILEDEXENAME].exe < [FILENAME]

:)

TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

pBe

  • Bull Frog
  • Posts: 402
Re: getting initals from a sting in lisp
« Reply #5 on: August 20, 2011, 05:48:45 AM »
Another one

Code: [Select]
(Defun _Initials (str)
(apply 'strcat
      (mapcar '(lambda (j)
               (substr (vl-symbol-name j) 1 1))
               (read (eval (strcat "(" str ")")))))
      )


Lee Mac

  • Seagull
  • Posts: 12916
  • London, England
Re: getting initals from a sting in lisp
« Reply #6 on: August 20, 2011, 07:41:45 AM »
Interesting approach pBe, no need for the 'eval' though  :wink:

pBe

  • Bull Frog
  • Posts: 402
Re: getting initals from a sting in lisp
« Reply #7 on: August 20, 2011, 09:40:44 AM »
(eval... )

I am wondering myself  :laugh:

Thanks Lee