Use of guards and sectioning

I start at Haskell, for my homework I have to write fuctions in 2 versions: with and without sectioning. One of these functions should return True if the argument is greater than 100, and False otherwise.

A non-partitioning function works well:

f5 x | x>100 = True
     | otherwise = False

But the second version does not:

f5' | (>100) = True
    | otherwise = False

Can you tell me how to write this function correctly?

+4
source share
2 answers

Your function without sectioning is too complex and causes you problems. It should be

f5 x = x > 100

Because the

(>) :: Ord a => a -> a -> Bool

a Bool. . : x > 100 - True, True, False, False. , .

x:

f5 = (> 100)

,

(> 100) :: (Ord a, Num a) => a -> Bool

- , Bool, a Bool. , , , Bool.

+5

. , , : .

{-# LANGUAGE ViewPatterns #-}

-- Don't ever do this :)
f5 ((> 100) -> True) =  True
f5 _                 = False

, , "" . , .

+1

Source: https://habr.com/ru/post/1694373/


All Articles