Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: new to lisp, need help.

178 views
Skip to first unread message
Message has been deleted

Kaz Kylheku

unread,
Sep 17, 2012, 2:27:17 PM9/17/12
to
On 2012-09-17, mchu...@gmail.com <mchu...@gmail.com> wrote:
> Assumptions:
> You can assume the following:
> 1. AND is the only word that can create compound sentences.
> 2. Any time that AND is used it is a compound sentence.
> 3. A compound sentence can only have 1 AND in it.
>
> Sample Output:
> Here is sample output for the function.
> Break 10 [14]> (BREAK_COMP '(I like you))
> ((I LIKE YOU))
> Break 10 [14]> (BREAK_COMP '(I like you and you like me))
> ((I LIKE YOU) (YOU LIKE ME))

(defun break-comp (list)
(let* ((and-clause (member 'and list))
(main-clause (ldiff list and-clause)))
`(,main-clause ,@(if and-clause `(,(cdr and-clause))))))


Uncool version, without backquote notation:

(defun break-comp (list)
(let* ((and-clause (member 'and list))
(main-clause (ldiff list and-clause)))
(if and-clause
(list main-clause and-clause)
(list main-clause))))

Zach Beane

unread,
Sep 17, 2012, 2:27:23 PM9/17/12
to
mchu...@gmail.com writes:

> Sometimes people write compound sentences which have two statements in
> one sentence. For example I like you and you like me. I would like
> you to write a function called BREAK_COMP that will accept a sentence
> and if the the sentence is a compound sentence return a list
> containing a list for each statement. If the sentence is not a
> compound sentence return a list of the sentence within a list.
>
> Assumptions:
> You can assume the following:
> 1. AND is the only word that can create compound sentences.
> 2. Any time that AND is used it is a compound sentence.
> 3. A compound sentence can only have 1 AND in it.
>
> Sample Output:
> Here is sample output for the function.
> Break 10 [14]> (BREAK_COMP '(I like you))
> ((I LIKE YOU))
> Break 10 [14]> (BREAK_COMP '(I like you and you like me))
> ((I LIKE YOU) (YOU LIKE ME))
>
>
> Would anyone care to show me one way they would implement this? The program itself is supposed to be quite short. I have never used lisp until now and I was only exposed to it a couple weeks ago.

Good to see some schools still teaching Lisp! Which school is it?

Here's one option:

(ql:quickload "split-sequence")

(defun break-comp (sentence)
(split-sequence:split-sequence-if (lambda (word) (eql word 'and))
sentence))

Zach

Pascal J. Bourguignon

unread,
Sep 17, 2012, 2:29:23 PM9/17/12
to
mchu...@gmail.com writes:

> Sometimes people write compound sentences which have two statements in
> one sentence.
>
> For example I like you and you like me. I would like you to write a
> function called BREAK_COMP that will accept a sentence and if the the
> sentence is a compound sentence return a list containing a list for
> each statement. If the sentence is not a compound sentence return a
> list of the sentence within a list.
>
> Assumptions:
> You can assume the following:
> 1. AND is the only word that can create compound sentences.
> 2. Any time that AND is used it is a compound sentence.
> 3. A compound sentence can only have 1 AND in it.
>
> Sample Output:
> Here is sample output for the function.
> Break 10 [14]> (BREAK_COMP '(I like you))
> ((I LIKE YOU))
> Break 10 [14]> (BREAK_COMP '(I like you and you like me))
> ((I LIKE YOU) (YOU LIKE ME))
>
>
> Would anyone care to show me one way they would implement this? The
> program itself is supposed to be quite short. I have never used lisp
> until now and I was only exposed to it a couple weeks ago.

You start with a grammar, and then write a parser.

From your rules, we have the following grammar:

sentence ::= { word } [ 'AND' {word } .
word ::= anything but 'AND'.

To keep it simple, I'll use the lisp reader as a scanner; the input to my
parser will be a list of symbol tokens.


(defun parse-sentence (tokens)
(let ((first-statement
(loop
:while (and tokens (not (eq 'and (first tokens))))
:collect (pop tokens))))
(if tokens
(progn
(pop tokens) ; eat AND.
(when (find 'and tokens)
(error "Not a sentence (two AND)"))
(list first-statement tokens))
(list first-statement))))


(defun break_comp (tokens)
(parse-sentence tokens))


cl-user> (break_comp '(hello world))
((hello world))
cl-user> (break_comp '(hello world and good bye tony))
((hello world) (good bye tony))
cl-user> (break_comp '(hello world and good bye tony and that's all))
> Debug: Not a sentence (two AND)


--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
Message has been deleted

Zach Beane

unread,
Sep 17, 2012, 3:09:16 PM9/17/12
to
mc <mchu...@gmail.com> writes:

> It's a private college in Maryland. It's a Programming Languages
> class, so we're going over the significant languages in the field.
>
> Thanks so much for your reply, as well as everyone else's. It helps a
> lot.

Neat. Well, keep in mind that what is being taught is probably not much
like how people use Lisp today.

Zach

kent...@gmail.com

unread,
Sep 23, 2012, 2:22:13 AM9/23/12
to

> It's a private college in Maryland. It's a Programming Languages class, so we're going over the significant languages in the field.

Awesome! Some prof in MD thinks Lisp is significant! That's a nice step up from dead.

-kt

WJ

unread,
Oct 17, 2012, 7:26:37 AM10/17/12
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
kent...@gmail.com wrote:

> Some prof in MD thinks Lisp is significant!

It seems he thinks DARPA CL is significant, not Lispy languages.

WJ

unread,
Oct 17, 2012, 7:30:26 AM10/17/12
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Racket (using pattern-matching):

(define (break-comp sentence)
(match sentence
[(list A ... 'and B ...) (list A B)]
[_ (list sentence)]))


: (break-comp '(I like you))
'((I like you))
: (break-comp '(I like you and you like me))
'((I like you) (you like me))

WJ

unread,
Jan 6, 2015, 3:31:03 AM1/6/15
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Gauche Scheme:

(use srfi-1)
(define (break-comp lst)
(define-values (a b) (break (cut eq? <> 'and) lst))
(cond-list (#t a) ((pair? b) (cdr b))))

gosh> (break-comp '(be here))
((be here))
gosh> (break-comp '(be here and go hence))
((be here) (go hence))

WJ

unread,
Jan 18, 2015, 8:24:27 AM1/18/15
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
elisp:

(require 'dash)

(defun break-comp (list)
(-split-on 'and list))

(break-comp '(be here))
===>
((be here))

(break-comp '(be here and go hence))
===>
((be here) (go hence))

(break-comp '(be here and go hence and stay there))
===>
((be here) (go hence) (stay there))


WJ

unread,
Jan 8, 2016, 4:21:09 AM1/8/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
MatzLisp (Ruby):

%w(be here and go hence and stay there).slice_before("and").to_a
===>
[["be", "here"], ["and", "go", "hence"], ["and", "stay", "there"]]

--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunkers-5.html
https://www.youtube.com/watch?v=EEl_1HWFRfo
0 new messages