Hutton's Razor is a minimal interpreted language the consists of two things: literal integers and the ability to add the them together.
- Find a way to express the basic Abstract Syntax Tree of Hutton's Razor. For example, in Haskell we can use a recursive sum type:
data Razor
= Lit Int
| Add Razor Razor
- Create a function that intrepts the different parts that make up the
Razor
data type. Again, in Haskell:
interpret (Lit 2)
-- returns 2
interpret (Add (Lit 1) (Lit 2))
-- returns 3
- Create another function that will return a string representation of the
Razor
it is given. More Haskell,
pretty (Lit 2)
-- returns "2"
pretty (Add (Lit 1) (Lit 2))
-- returns "( 1 + 2 )"
See if you can write this in an OOP way, interfaces, classes, etc. Once you have both, add the following:
- Using the existing AST for a
Razor
add some new behavior, a new function. - Modify the AST to express multiplication.
How does each paradigm (FP and OOP) effect the adding of new behavior and the adding of a new "type?". This is the basic idea of The Expression Problem.
This kata was taken from Code Wars.
Given a pyramid with this basic shape:
3
7 4
2 4 6
8 5 9 3
Determine the "longest slide down" which is defined as
[The] sum of consecutive numbers from the top to the bottom of the pyramid.
So the pyramid above gives us this path as the "longest slide":
/3/
\7\ 4
2 \4\ 6
8 5 \9\ 3
3
because, well that's the only choice. The next step in the path is 7
or 4
both are "connected" to 3
and 7
is the largest of the two. Next we have 2
, 4
and 6
but 6
is not "connected" to the 7
we selected before so we are left to choose between 2
and 4
. The final row follows the same pattern.
So, longestSlideDown([[3], [7, 4], [2, 4, 6], [8, 5, 9, 3]])
should return 23
.
The challenge is to write a longestSlideDown
function; you can assume that the nested list will be sorted...but it might be more fun to not make that assumption and sort the outer list yourself, IDK.
This kata was taken from Coding Dojo who took it from Robert Martin.
Write a function, wrap
that takes two arguments, a string, and a column number. The function returns the string, but with line breaks inserted in the right places based column number passed to the function. Make sure that no line is longer than the column number and only break lines at word boundaries.
Like a word processor, break the line by replacing the last space in a line with a newline.