Code Monkey home page Code Monkey logo

lambdaenigma.com's People

Contributors

rxdave avatar

Stargazers

 avatar

Watchers

 avatar

lambdaenigma.com's Issues

Puzzle #0 Solution

Lambda Enigma

Confused? Don't sweat it. The concept of this game is new to everyone!
Here are some helpful tips.

  1. Your solution must be a lambda expression of the form: (parameter list) => body
    1. However, if your expression only has a single parameter, then you may omit the parenthesis:
      p => body
  2. You must use all of the functions defined above the text area within your solution. These functions are the "pieces" of the puzzle.
  3. You cannot use literal expressions in your solution. Using literal numbers or strings, for example, will always result in a program error.
    1. TIP: If you think that you need to use a literal, then see if any of the puzzle's pieces include literals for you! It's safe to use pieces that use literals, you just cannot enter them yourself.

Puzzle #0

This is an introductory puzzle that is meant to help you get started.

Pieces

This puzzle only has a single piece that you must use in your solution. This piece is a function named d and its definition is displayed on top of the puzzle:

d = x => x * 2

The function's name, d, is shown to the left of the equals sign (=). To the right is the piece's function.

x => x * 2

It can be read out loud as, "x goes to x times two".

This function returns double its input. If you pass it 2, then it returns 4. If you pass it 5, then it returns 10.

To use this piece in your solution, you could apply d to some argument, x. We'll see how in a moment.

Input

The value to the left, 21, is the input to the lambda expression that you must write to solve this puzzle.

So, let's start by binding the input to a parameter. We'll call it input, although we can use any letter or combination of letters and numbers (as long as it starts with a letter).

input

Another typical name to use for the input parameter is simply x.

Before we continue, we'll write the lambda arrow to separate the parameter list from the body of our expression:

input => 

And now we must write the body of our lambda expression. This is the part that returns a value using all of the pieces of the puzzle, which are defined above the puzzle. In this puzzle, we have only one piece: d.

Before we can use d, let's see what our lambda expression must return!

Output

The value to the right, 42, is the value that our lambda expression must return in order for our solution to be correct.

So let's take account of what we know:

  1. 21 goes in
  2. 42 must come out
  3. We must use d, which doubles any input that it's given.

So let's apply d to input!

input => d(input)

Another Solution

There's another way to solve this puzzle, but it's somewhat subtle.

Take a look at the definition of d again. Let's try to infer its data types and thus its signature.

d = x => x * 2

We know that d returns a number, due to the use of the multiplication operator * and the literal value 2. We can also infer that x must be a number, since it's being multiplied.

Perhaps the definition of d could be written like this:

d: int -> int

In other words, d takes an integer and returns an integer.

Now let's examine the signature of the function we must write to solve puzzle #0.

  1. First, we know the input is 21, an integer.
  2. Second, we know the output is 42, another integer.

Therefore, the definition is the following:

int -> int

Well, that's identical to the definition of d!
Therefore, there's a very simple solution to this puzzle:

d

Try it yourself!

Puzzle #10 Discussion

Puzzle 10

I got to level 10, but I think the "If" implementation is not working. Or it could very well be me.
The editor tells me:

  • True(n, n)
    is a valid expression (it returns n). But not using all the functions. Makes sense
  • If(True, n, n)
    Argument type mismatch!

given the definition of If: (p, a, b) => p(a, b)
I don't understand why the second expression gives an argument mismatch ? it should be directly equivalent to the first

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.