简体   繁体   中英

Creating basic functions in Haskell

I'm new to Haskell and would like a few pointers on creating functions. I am used to writing in C#, Java, and C++, so this stuff is really foreign to me.

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. Write two versions of this function: first use if else and second use guards.

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)

I am using 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. Thank you very much.

Updates: -added else to code -fixed last if statement

Screenshot:

在此处输入图片说明

Adding to J's answer, I've gone ahead and implemented your function. This changes your function to use exhaustive patterns (as J mentioned), and also corrects the type signature, which only accepted 2 Ints. With if statements:

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:

{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.

Everything in Haskell is an expression and thus must result in a value. The code you wrote involves three "half"- if s, none of which resolve to a value. For instance, consider just one "half"- if :

if (a > b) then c

We can replace the (a > b) with both True and False to see what happens. If we use True it's clear that the value is

if True  then c ====> c

But if we use False

if False then c ====> ?

In other words, "half"- if s don't make much sense if their condition evaluates to False . Thus, in Haskell, all if s must be "full" if s—ie they must always have then and else branches.

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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