Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多项式的文本表示的F#_F# - Fatal编程技术网

多项式的文本表示的F#

多项式的文本表示的F#,f#,F#,我需要以下方面的帮助: 假设多项式由 a_0+a_1x+a_2x^2+ 并且系数存储在列表[a_0;a_1;a_2;…]中 我想写一个F#函数,它给出多项式的文本表示。 它可能是递归的,但我现在看不出有什么效果。任何输入?如果您这样做是为了学习F#(或作为家庭作业),那么最好使用递归。您可以从列表递归处理的常用模板开始—在该模板中迭代系数、处理空列表和处理非空列表 下面显示了模板,但它会留下身体的其余部分进行填充,因此您将面临一些挑战: let coefficients = [5;2;7;4]

我需要以下方面的帮助: 假设多项式由

a_0+a_1x+a_2x^2+

并且系数存储在列表[a_0;a_1;a_2;…]中

我想写一个F#函数,它给出多项式的文本表示。
它可能是递归的,但我现在看不出有什么效果。任何输入?

如果您这样做是为了学习F#(或作为家庭作业),那么最好使用递归。您可以从列表递归处理的常用模板开始—在该模板中迭代系数、处理空列表和处理非空列表

下面显示了模板,但它会留下身体的其余部分进行填充,因此您将面临一些挑战:

let coefficients = [5;2;7;4]

let rec format exponent coefficients = 
  match coefficients with 
  | [] -> ""
  | c::coefficients ->
      let rest = format (exponent + 1) coefficients
      // Now we have 'c', which is the current coefficient,
      // 'exponent', which is the current exponent and 
      // 'rest' which is the string representing the rest
      // of the polynomial. For example, for the sample above
      // we will at some point have:
      //
      //   rest = "4x^3"
      //   exponent = 2
      //   c = 7
      //
      // What code do we need to construct and return the 
      // final string, i.e. "7x^2 + 4x^3" ??
为了做好这一点,您可能还需要添加一些内容:

  • 如果
    c=0
    ,则您希望删除该组件,而不是添加
    0x^3
  • 如果
    c=1
    ,则只需添加
    x^3
  • 在最后一个系数
    [c]
    中添加一个案例处理会更容易(这样,您就不会在最后得到一个无用的
    +

如果您这样做是为了学习F#(或作为家庭作业),那么使用递归是个好主意。您可以从列表递归处理的常用模板开始—在该模板中迭代系数、处理空列表和处理非空列表

下面显示了模板,但它会留下身体的其余部分进行填充,因此您将面临一些挑战:

let coefficients = [5;2;7;4]

let rec format exponent coefficients = 
  match coefficients with 
  | [] -> ""
  | c::coefficients ->
      let rest = format (exponent + 1) coefficients
      // Now we have 'c', which is the current coefficient,
      // 'exponent', which is the current exponent and 
      // 'rest' which is the string representing the rest
      // of the polynomial. For example, for the sample above
      // we will at some point have:
      //
      //   rest = "4x^3"
      //   exponent = 2
      //   c = 7
      //
      // What code do we need to construct and return the 
      // final string, i.e. "7x^2 + 4x^3" ??
为了做好这一点,您可能还需要添加一些内容:

  • 如果
    c=0
    ,则您希望删除该组件,而不是添加
    0x^3
  • 如果
    c=1
    ,则只需添加
    x^3
  • 在最后一个系数
    [c]
    中添加一个案例处理会更容易(这样,您就不会在最后得到一个无用的
    +

我认为问题不在于这个问题“太简单”。只要你知道怎么做,一切都很容易。问题是这个问题没有显示出解决问题的努力。然而,直接的解决方案可能是这样的:
let c=[5;4;3;2;1]
let f=c |>List.mapi(fun i x->sprintf“%ix^%i”x i)|>List.reduce(fun x y->x+“+”+y)
这不处理x^0=1的情况,因此它仍然显示为:“5x^0+4x^1+3x^2+2x^3+1x^4”我不认为这里的问题是这个问题“太容易了”。只要你知道怎么做,一切都很容易。问题是这个问题没有显示出解决问题的努力。然而,直接的解决方案可能是这样的:
let c=[5;4;3;2;1]
let f=c |>List.mapi(fun i x->sprintf“%ix^%i”x i)|>List.reduce(fun x y->x+“+”+y)
这不处理x^0=1的情况,因此它仍然显示为:“5x^0+4x^1+3x^2+2x^3+1x^4”