Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement let expression #14

Open
cowboy8625 opened this issue Oct 22, 2023 · 0 comments
Open

Implement let expression #14

cowboy8625 opened this issue Oct 22, 2023 · 0 comments
Labels
compiler compiler related activity enhancement New feature or request parser work in the parser

Comments

@cowboy8625
Copy link
Owner

Example 1

next_cell grid x
  : Array<String> -> Int -> Bool
    let len = length grid in
    let a = nth grid (idx_of (x - 1) len) in
    let b = nth grid x in
    let c = nth grid (idx_of (x + 1) len) in
      is_alive a b c

Example 2

next_cell grid x
  : Array<String> -> Int -> Bool
  = (λlen ->
    (λa   ->
    (λb   ->
    (λc   -> is_alive a b c)
    (nth grid (idx_of (x - 1) len)))
    (nth grid x))
    (nth grid (idx_of (x + 1) len)))
    (length grid)

In the first version using let expressions, the code is structured to make it more readable and easier to understand. It follows a sequence of steps to achieve the desired result:

  1. let len = length grid calculates the length of the grid and assigns it to the variable len.
  2. let a = nth grid (idx_of (x - 1) len) retrieves the element at the position x - 1 within the grid and assigns it to the variable a.
  3. let b = nth grid x retrieves the element at the position x within the grid and assigns it to the variable b.
  4. let c = nth grid (idx_of (x + 1) len) retrieves the element at the position x + 1 within the grid and assigns it to the variable c.
  5. Finally, the result of is_alive a b c is returned, indicating whether the cell at position x is alive or not.

The second version uses lambda expressions to achieve the same result. It explicitly defines nested anonymous functions, passing arguments between them:

  1. (λlen -> ...) defines a lambda function that takes the len argument.
  2. (λa -> ...) defines a lambda function that takes the a argument.
  3. (λb -> ...) defines a lambda function that takes the b argument.
  4. (λc -> is_alive a b c) defines a lambda function that takes the c argument and uses the a, b, and c values to call the is_alive function.

In both cases, the code structures the logic of checking cell status based on its neighbors, but the first version with let expressions provides a more natural, sequential representation of the same computation, making it easier to read and understand.

@cowboy8625 cowboy8625 added enhancement New feature or request parser work in the parser compiler compiler related activity labels Oct 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler compiler related activity enhancement New feature or request parser work in the parser
Projects
None yet
Development

No branches or pull requests

1 participant