I see a lot of ``faggots'' here claiming to have achieved Satori and becoming [b][i][o][u]EXPERT PROGRAMMER[\u][\o][\i][\b]s after reading SICP, but I have yet to single /prog/rider demonstrate a working program they've built. Show us what you and Lisp can do. I'm legitimately curious.
Name:
any questions?2011-08-12 0:06
Lisp:
// Usage: generate {sentence}
Grammar -> split {|} {sentence -> (noun_phrase verb_phrase)
|noun_phrase -> (Article Noun)
|verb_phrase -> (Verb noun_phrase)
|Article -> the a
|Noun -> man ball woman table
|Verb -> hit took saw liked}
grammar = Dict(
S = [['NP','VP']],
NP = [['Art', 'N']],
VP = [['V', 'NP']],
Art = ['the', 'a'],
N = ['man', 'ball', 'woman', 'table'],
V = ['hit', 'took', 'saw', 'liked']
)
def generate(phrase):
"Generate a random sentence or phrase"
if isinstance(phrase, list):
return mappend(generate, phrase)
elif phrase in grammar:
return generate(choice(grammar[phrase]))
else: return [phrase]
def generate_tree(phrase):
"""Generate a random sentence or phrase,
with a complete parse tree."""
if isinstance(phrase, list):
return map(generate_tree, phrase)
elif phrase in grammar:
return [phrase] + generate_tree(choice(grammar[phrase]))
else: return [phrase]
def mappend(fn, list):
"Append the results of calling fn on each element of list."
return reduce(lambda x,y: x+y, map(fn, list))