简体   繁体   English

在Haskell中创建基本功能

[英]Creating basic functions in Haskell

I'm new to Haskell and would like a few pointers on creating functions. 我是Haskell的新手,并希望提供一些有关创建函数的提示。 I am used to writing in C#, Java, and C++, so this stuff is really foreign to me. 我习惯于用C#,Java和C ++编写,所以这些东西对我来说真的很陌生。

Here is the question that I am working on: 这是我正在研究的问题:

Declare the type and write a function in Haskell that takes three numbers as inputs and returns the larger one. 声明类型并在Haskell中编写一个函数,该函数将三个数字作为输入并返回较大的一个。 Write two versions of this function: first use if else and second use guards. 编写此函数的两个版本:第一次使用else,第二次使用防护。

In working on the first one I've come up with something like this...: 在编写第一个文件时,我想到了以下内容:

largestInt :: Int -> Int -> Int --Declaring type
largestInt a b c = if (a > b && a > c) then a --I don't think this is right, but it's all I have
                   else if (b > a && b > c) then b
                   else c

I've been using this video for help, but am not able to do the same things as him for some reason. 我一直在使用视频寻求帮助,但由于某种原因无法执行与他相同的操作。 (Meaning that I can't create that Main.hs file) (意味着我无法创建该Main.hs文件)

I am using WinGHCi 我正在使用WinGHCi

I would appreciate it if someone could show me how to setup functions, maybe even this one ^ in Haskell so that I know what I'm doing in the future. 如果有人可以向我展示如何设置功能,甚至包括Haskell中的这个^,以使我知道我将来在做什么,我将不胜感激。 Thank you very much. 非常感谢你。

Updates: -added else to code -fixed last if statement 更新:-else到代码中-修复了最后的if语句

Screenshot: 屏幕截图:

在此处输入图片说明

Adding to J's answer, I've gone ahead and implemented your function. 除了J的答案,我已经继续执行您的功能。 This changes your function to use exhaustive patterns (as J mentioned), and also corrects the type signature, which only accepted 2 Ints. 这会将您的函数更改为使用穷举模式(如J所述),并更正仅接受2个Ints的类型签名。 With if statements: 使用if语句:

largestInt :: Int -> Int -> Int -> Int
largestInt a b c = if (a >= b && a >= c) 
                       then a 
                          else if (b >= a && b >= c)
                            then b 
                              else c  

And with guards (which is a much nicer way to do this, by the way): 和警卫(顺便说一句,这是一种更好的方法):

largestInt :: Int -> Int -> Int -> Int
largestInt a b c
    | (a >= b && a >= c) = a 
    | (b >= a && b >= c) = b
    | otherwise = c

If you were to put this directly into GHCI, you would do: 如果将其直接放入GHCI中,则可以执行以下操作:

{largestInt :: Int -> Int -> Int -> Int;largestInt a b c ; | (a > b && a > c) = a |(b > a && b > c) = b |otherwise = c}

You may need to add semi-colons before each guard, but I believe it doesn't matter. 您可能需要在每个后卫之前添加分号,但是我认为这没有关系。 This is the general syntax for defining multi-line functions in GHCI. 这是在GHCI中定义多行函数的通用语法。

Everything in Haskell is an expression and thus must result in a value. Haskell中的所有内容都是表达式,因此必须产生值。 The code you wrote involves three "half"- if s, none of which resolve to a value. 您编写的代码涉及三个“ half”( if s),它们都不能解析为值。 For instance, consider just one "half"- if : 例如,仅考虑一个“一半”- if

if (a > b) then c

We can replace the (a > b) with both True and False to see what happens. 我们可以将(a > b)替换为TrueFalse以查看会发生什么。 If we use True it's clear that the value is 如果我们使用True则显然该值为

if True  then c ====> c

But if we use False 但是如果我们使用False

if False then c ====> ?

In other words, "half"- if s don't make much sense if their condition evaluates to False . 换句话说,如果条件的值为False ,则“ half”- if没有太大意义。 Thus, in Haskell, all if s must be "full" if s—ie they must always have then and else branches. 因此,在Haskell中, if s的所有if必须为“ full” if即它们必须始终具有then else分支。

if (a > b && a > c) 
  then a
  else if (b > a && b > c)
    then b
    else if (c > a && c > b)
      then c
      else ...?

Part of the reason Haskell requires "full" if s is outlined in this last example—it helps to avoid errors in reasoning like the above where we cannot be certain that the third branch will always be true unless we check it. if在最后一个示例中概述了s,则Haskell要求“完整”的部分原因-有助于避免上述推理中的错误,除非我们不进行检查,否则无法确定第三个分支始终为真。

In fact, what you've written would have to fall through all the way to the end for something like 实际上,您所写的内容必须一路走到尽头,例如

largestInt 0 0 0

Since no argument is actually the largest at all! 因为实际上没有论据是最大的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM