Pig Latin in C and Lex, meet LISP!

aark at ihuxe.UUCP aark at ihuxe.UUCP
Sat Sep 3 07:33:53 AEST 1983


First, Mr. Art Zemon's English-to-Pig-Latin translator, written
using Lex, does not translate correctly, at least according to
the Pig Latin I learned:
	- Words beginning with a vowel are translated by appending
	"way", not "yay".
	- Sometimes an initial "y" is a vowel, as in "yttrium," and
	must be treated as such.  Zemon's translator does not.
	- For words that begin with more than one consonant, ALL
	these consonants, not just the first, are put at the end,
	then "ay" is appended.  (An initial "qu" is a special case
	that Zemon's translator does not handle either.)

Second, this problem can be solved with an even shorter and easier
program if you use a language suited to the problem, namely Lisp.

(defun englishtopig (sentence)
  (mapcar 'etop1 sentence)
)
(defun etop1 (word)
  (implode (etop2 (explode word)))
)
(defun etop2 (letterlist)
  (cond
	((and (equal (car letterlist) '@) (vowel (cadr letterlist)))
	  (append (cdr letterlist) '(ay)))
	((equal (car letterlist) '@)
	  (etop2 (append '(@) (cddr letterlist) (list (cadr letterlist)))))
	((and (equal (car letterlist) 'q) (equal (cadr letterlist) 'u))
	  (append (cddr letterlist) '(quay)))
	((and (equal (car letterlist) 'y) (not (vowel (cadr letterlist))))
	  (append letterlist '(way)))
	((vowel (car letterlist))
	  (append letterlist '(way)))
	(t
	  (etop2 (cons '@ letterlist)))
  )
)
(defun vowel (letter)
  (or
	(equal letter 'a)
	(equal letter 'e)
	(equal letter 'i)
	(equal letter 'o)
	(equal letter 'u)
  )
)

You can't read this program, you say?  Maybe you should learn Lisp.
You can write many (not all) programs a lot more easily in Lisp than in C,
even C augmented with code generators like Lex, and this is one of them.

-- 
	Alan R. Kaminsky
	Bell Laboratories, Naperville, IL
	...ihnp4!ihuxe!aark



More information about the Comp.sources.unix mailing list