Elixir:为生成器分配变量(变量范围?)

时间:2018-02-06 07:21:50

标签: for-loop scope elixir variable-assignment

我正在解决,find the largest prime factor of the number, Project Euler problem3

遵循Elixir代码抛出警告,并且不评估if block(赋值)我认为:

num = 13195

range = num
         |> :math.sqrt
         |> Float.floor
         |> round

for dv <- 2..range do
  if rem(num, dv) == 0 and div(num, dv) != 1 do
    num = div(num, dv)
  end
end

num
|> IO.puts

警告是:

$ elixir 3.exs
warning: variable "num" is unused
  3.exs:10

warning: the result of the expression is ignored (suppress the warning by assigning the expression to the _ variable)
  3.exs:10

13195

$ elixir -v
Erlang/OTP 20 [erts-9.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Elixir 1.5.3

如何更新(分配)num? (以下Python和Javascript代码同样适用于此问题):

# 3.py
from math import ceil, sqrt

num = 600851475143

for div in range(2, ceil(sqrt(num)) + 1):
    if num%div == 0 and num/div != 1:
        num /= div

assert int(num) == 6857
// 3.js
var num = 600851475143;

var range = Array.from({length: Math.trunc(Math.sqrt(num))}, (x, i) => i + 2)

for (const div of range) {
  if (num%div === 0 && num/div != 1) {
      num /= div;
  }
}

var assert = require('assert');

assert(num === 6857)

1 个答案:

答案 0 :(得分:2)

您实际上正在创建一个新变量并从外部范围隐藏

你可以像这样重写它

num = 13195

range =
  num
  |> :math.sqrt()
  |> Float.floor()
  |> round

num =
  2..range
  |> Enum.reduce(num, fn elem, acc ->
    if rem(acc, elem) == 0 and div(acc, elem) != 1 do
      div(acc, elem)
    else
      acc
    end
  end)

IO.puts num

有关遮蔽的更多信息:

+------------------------------------------------------------+
| Top level                                                  |
|                                                            |
|  +------------------------+     +------------------------+ |
|  | Module                 |     | Module                 | |
|  |                        |     |                        | |
|  | +--------------------+ |     | +--------------------+ | |
|  | | Function clause    | |     | | Function clause    | | |
|  | |                    | |     | |                    | | |
|  | | +----------------+ | |     | | +----------------+ | | |
|  | | | Comprehension  | | |     | | | Comprehension  | | | |
|  | | +----------------+ | |     | | +----------------+ | | |
|  | | +----------------+ | | ... | | +----------------+ | | |
|  | | | Anon. function | | |     | | | Anon. function | | | |
|  | | +----------------+ | |     | | +----------------+ | | |
|  | | +----------------+ | |     | | +----------------+ | | |
|  | | | Try block      | | |     | | | Try block      | | | |
|  | | +----------------+ | |     | | +----------------+ | | |
|  | +--------------------+ |     | +--------------------+ | |
|  +------------------------+     +------------------------+ |
|                                                            |
+------------------------------------------------------------+
     

嵌套作用域中的任何变量,其名称与周围作用域中的变量一致,将遮蔽该外部变量。换句话说,嵌套作用域内的变量暂时隐藏了周围作用域中的变量,但不会以任何方式影响它。

source