solutions-learn-physics-with-fp-0.1.0.0
Copyright(c) Eric Zoerner 2023
LicenseBSD3
Maintainereric.zoerner@proton.me
Safe HaskellSafe-Inferred
LanguageGHC2021

Chapter03

Description

 
Synopsis

Exercise 3.1

(a) (False || (True && False)) || True

:: Bool

(b) ((2 3) 4) == ((4 3) 2)

:: Bool

(c) ((7 - (5 / 4)) > 6) || (((2 ^ 5) - 1) == 31)

:: Bool

(d) 2 < 3 < 4

Not well formed: cannot operate on Bool with comparison operators.

(e) (2 < 3) || (3 < 4)

:: Bool

(f) 2 && 3 < 4

Not well formed: cannot operate on numeric types with logical && operator.

Exercise 3.2

f :: Double -> Double Source #

f(x) = { 0, x ≤ 0; x, x > 0}

e :: Double -> Double Source #

E(r) = { r, r ≤ 1; 1/r², r > 1}

Exercise 3.3

isXorY :: Char -> Bool Source #

Returns True if the input character is 'X' or 'Y' or else False.

>>> isXorY 'X'
True
>>> isXorY 'Y'
True
>>> isXorY 'Z'
False

Exercise 3.4

bagFee :: Bool -> Int Source #

Returns 100 if the person is checking bags and 0 if not. Implemented with if-then-else.

bagFee2 :: Bool -> Int Source #

Returns 100 if the person is checking bags and 0 if not. Implemented with pattern matching on the input.

Exercise 3.5

greaterThan50 :: Integer -> Bool Source #

Returns True if the given integer is greater than 50 or else False

Exercise 3.6

amazingCurve :: Int -> Int Source #

Double the score without going over 100

Exercise 3.7

>>> :type bagFee False
bagFee False :: Int
>>> bagFee False
0

Exercise 3.8

circleRadius :: Double Source #

circleRadius = 3.5

cot :: Double -> Double Source #

cot x = 1 / tan x

fe :: Double -> Double Source #

fe epsilon = epsilon * tan (epsilon * pi / 2)

fo :: Double -> Double Source #

fo epsilon = -epsilon * cot (epsilon * pi / 2)

g :: Double -> Double -> Double Source #

g nu epsilon = sqrt (nu ** 2 - epsilon ** 2)

Exercise 3.9

How many functions with type Bool -> Bool are there?

There are 4 Bool -> Bool functions.

What would be good names for them?

alwaysFalse, alwaysTrue, identity, and not

How many functions have type Bool -> Bool -> Bool?

There are 16 Bool -> Bool -> Bool functions.

Source: The 16 Boolean Logic Functions of Two-Input Systems

Exercise 3.10

True || False && False