具有较高范围的多个输入值 [英] Multiple input values with upper range

查看:177
本文介绍了具有较高范围的多个输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我需要一点帮助,或者至少有一个正确的方向!我是Haskell的新手,但我熟悉C#和PHP。



我试图创建一个FizzBu​​zz函数,允许输入3个参数。我了解FizzBu​​zz的一般概念,但我试图创建一个函数,允许您将第一个除数,第二个除数和最后一个参数作为上限。我努力理解的部分是如何将输入值分配给函数中的变量。



我找到了不同的教程来展示如何在Haskell中执行常规的FizzBu​​zz。



所以我的主要问题是:


  1. 您将输入值分配给变量?我知道如何分配类型,但是我不知道如何在函数中引用它。

      fz':: [Integer,Integer,Integer]  - >整数 - >整数 - >整数


  2. 从我在线阅读的内容中,一个大功能来执行一切。有人说,最好是有:

    a。一个函数接收输入值并赋值变量,然后调用单独的函数?

    b。在单独的函数中,设置范围,然后执行 divideBy mod 以检查值如果 x 和<可以被 [1..z] 打印,如果 y code> y 可以被 [1..z] 打印fizzbuzz $ c>可被 [1..z] print buzz整除?使用其中子句或 case



    ℃。用于实现范围值和(x,y,z)?..


$的独立函数b
$ b

任何想法,提示和帮助? 解决方案



 函数abc($ x,$ y){...} 



在Haskell中,您可以这样做:

  abc xy = ... 

如果您想添加类型签名:

  abc :: Int  - >字符串 - >无论什么
abc xy = ...

Haskell版本的广义Fizz-buzz可能定义如下:

  myFizzBu​​zz :: Int  - > Int  - > Int  - > [String] 
myFizzBu​​zz div1 div2 upperBound =
- 这里`div1`是第一个除数,`div2`第二个
- 和`upperBound`是范围的上限
...
- 返回一个字符串列表,例如[Fizz,Fizz,Buzz,...]

请注意 myFizzBu​​zz 返回一个字符串列表 - 它不打印出来。要将它们打印出来,只需将 putStrLn 加在一起:

  printMyFizzBu​​zz div1 div2 upperBound = 
putStrLn $ lines(myFizzBu​​zz div1 div2 upperBound)

这也说明了从计算中分离出的IO和Haskell程序中普遍存在的问题:您将程序构造为主要由薄IO层包围的纯粹计算。在这种情况下, myFizzBu​​zz 是您的纯函数。从控制台获取参数的完整程序可能如下所示:

  main = do 
(arg1:arg2 :arg3:_)< - fmap words getLine - IO层
let div1 =读取arg1 - 纯核心
div2 =读取arg2 - |
upperBound =读取arg3 - |
let results = myFizzBu​​zz div1 div2 upperBound - 纯核心
putStrLn $行结果 - IO层

编写纯函数的一个原因是它们往往更具可重用性。



不是在中指定上限myFizzBu​​zz 编写它以生成无限的Fizz和Buzz字符串系列:

  allFizzBu​​zz :: Int  - > Int  - > [String] 
allFizzBu​​zz div1 div2 = ...
- 除数div1,div2的无穷Fizz-Buzz序列,从1开始

然后, myFizzBu​​zz 就是 take 函数,由 allFizzBu​​zz

  myFizzBu​​zz div1 div2 upperBound = take upperBound(allFizzBu​​zz div1 div2) 


So I'm in need of a little assistance or at least a point in the right direction! I'm new to Haskell but I'm familiar with C# and PHP.

I'm trying to create a FizzBuzz function that allows 3 parameters to be entered. I understand the general concept of FizzBuzz but I'm trying to be able to create a function that allows you to put the first divisor, second divisor and the last parameter as an upper range. The part where I'm struggling to understand is how you assign input values to variables in a function.

I found different tutorials showing how to do the regular FizzBuzz in Haskell.

So my main questions would be this:

  1. How do you assign input values to variables? I know how to assign the type, which would be something like this but I don't know how you would reference it in a function.

    fz' :: [Integer, Integer, Integer] -> Integer -> Integer -> Integer
    

  2. From what I've read online, it's better to separate the functions instead of having one large function to perform everything. With that being said, would it be best to have:

    a. One function receive the input values and assign them variables, then call separate functions?

    b. In the separate functions, set the range and then do divideBy or mod to check if value x is divisible by [1..z] print fizz, if x and y are divisble by [1..z] print fizzbuzz, if y is divisible by [1..z] print buzz? Is it better to use a where clause or case?

    c. Separate function that implements the values for the range and (x,y,z)?..

Any ideas, tips, help?

解决方案

In PHP you define a function like this:

function abc($x, $y) { ... }

In Haskell you would do it this way:

abc x y = ...

and if you wanted to add a type signature:

abc :: Int -> String -> Whatever
abc x y = ...

A Haskell version of your generalized fizz-buzz might be defined like this:

myFizzBuzz :: Int -> Int -> Int -> [String]
myFizzBuzz    div1   div2   upperBound =
  -- here `div1` is the first divisor, `div2` the second
  -- and `upperBound` is the upper bound of the range
  ...
  -- return a list of strings, e.g. ["Fizz", "Fizz", "Buzz", ... ]

Note that myFizzBuzz returns a list of Strings - it doesn't print them out. To print them out just join the resulting words with lines and call putStrLn:

printMyFizzBuzz div1 div2 upperBound = 
  putStrLn $ lines (myFizzBuzz div1 div2 upperBound)

This also illustrates the separation of IO from computation which is prevalent in Haskell programs: you structure your program as mainly a pure computation surrounded by a thin IO-layer. In this case myFizzBuzz is your pure function. A complete program which takes it's parameters from the console might look like this:

main = do
  (arg1 : arg2 : arg3 : _) <- fmap words getLine  -- IO layer
  let div1 = read arg1                            -- pure core
      div2 = read arg2                            --  |
      upperBound = read arg3                      --  |
  let results = myFizzBuzz div1 div2 upperBound   -- pure core
  putStrLn $ lines results                        -- IO layer

One reason to write pure functions is that they tend to be more reusable.

Instead of specifying an upper bound in myFizzBuzz what about writing it to generate an infinite series of "Fizz" and "Buzz" strings:

allFizzBuzz :: Int -> Int -> [String]
allFizzBuzz div1 div2 = ...
  -- the infinite Fizz-Buzz sequence for divisors div1, div2 starting with 1

Then the myFizzBuzz is simply the take function composed with allFizzBuzz:

myFizzBuzz div1 div2 upperBound = take upperBound (allFizzBuzz div1 div2)

这篇关于具有较高范围的多个输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆