Skip to main content

Lec 2: Pig Latin Project

This is based on the rules for pig latin which I found on a pig latin translator website.

It demonstrates a recursive function.

#lang racket
(require (planet dyoo/simply-scheme:2:2))

;rules: https://lingojam.com/PigLatinTranslator
; If a word begins with a vowel, just as "yay" to the end. For example, "out" is translated into "outyay".
; If it begins with a consonant, then we take all consonants before the first vowel and we put them on the end of the word. For example, "which" is translated into "ichwhay".

;helpful
(define vowels (list 'a 'e 'i 'o 'u))
(define consonants (list 'b 'c 'd 'f 'g 'h 'j 'k 'l 'm 'n 'p 'q 'r 's 't 'v 'w 'x 'y 'z))

;functions
(define (piglify_vowels wd)
(word wd 'yay))

(define (piglify_consonants wd)
(if (member? (first wd) consonants)
(piglify_consonants (word (butfirst wd) (first wd)))
(word wd 'ay)))

(define (piglify wd)
(cond
((member? (first wd) vowels) (piglify_vowels wd))
(else (piglify_consonants wd))))

;testing
(define (is_equal? a b)
(if (equal? a b)
(sentence a b '=> '✅)
(sentence a b '=> '❌)))

(is_equal? (piglify 'out) 'outyay)
;"pig" = "igpay"
(is_equal? (piglify 'pig) 'igpay)
;"latin" = "atinlay"
(is_equal? (piglify 'latin) 'atinlay)
;"banana" = "ananabay"
(is_equal? (piglify 'banana) 'ananabay)
;"friends" = "iendsfray"
(is_equal? (piglify 'friends) 'iendsfray)
;"smile" = "ilesmay"
(is_equal? (piglify 'smile) 'ilesmay)
;"string" = "ingstray"
(is_equal? (piglify 'string) 'ingstray)
;"eat" = "eatway"
(is_equal? (piglify 'eat) 'eatyay)
;"omelet" = "omeletway"
(is_equal? (piglify 'omelet) 'omeletyay)
;"are" = "areway"
(is_equal? (piglify 'are) 'areyay)