15.08.2013 Views

General Computer Science; Problems and Solutions for ... - Kwarc

General Computer Science; Problems and Solutions for ... - Kwarc

General Computer Science; Problems and Solutions for ... - Kwarc

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Problem 1.16: Write a function that takes an odd positive integer <strong>and</strong> returns a char list list<br />

which represents a triangle of stars with n stars in the last row. For example,<br />

triangle 5;<br />

val it =<br />

[#" ", #" ", #"*", #" ", #" "],<br />

[#" ", #"*", #"*", #"*", #" "],<br />

[#"*", #"*", #"*", #"*", #"*"]]<br />

Solution:<br />

fun stars(0) = nil |<br />

stars(n) = #"*" :: stars(n-1)<br />

fun wall(nil) = nil |<br />

wall(hd::tl) = ((#" "::hd)@[#" "])::wall(tl)<br />

fun triangle(1) = [[#"*"]] |<br />

triangle(n) = wall(triangle(n-2))@[stars(n)];<br />

17

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!