-- CPSC 312 - 2024 - Our First Haskell Program
module First where

-- To run this program, do:
-- ghci
-- :load First

-- Equality Provides definitions
x = y+3
y = 10

-- functions can have arguments:
square x = x*x
fourth x = square (square x)

foo x y = 1000*x+10*y

-- try:
-- foo 9 3
-- foo 9 3+5


-- functions can be written with multiple definitions - first matching one succeeds
fac 0 = 1
fac n = n * fac (n-1)

--
myif True x y = x
myif False x y = y

myfac n = myif (n == 0) 1 (n * myfac (n-1))


-- Functions can also be infix (if don't contain letters or digits)
x %^&*$ y = 1000*x+10*y

--try
-- 6 %^&*$ 7
-- Infix functions can be made prefix by putting them in parentheses:
-- (%^&*$) 6 7
-- Prefix functions can be make infix by putting them between ` back-quotes
-- 6 `foo` 7



-- Haskell evaluates expressions
-- infix operators: * +&^%$# `foo`
-- prefix: div myfun (&^%$)
-- prefix functions have precedence; parentheses overide precedence

-- f x1 .. xn = e    defines function f
-- semantics:
-- f followed by n arguments
-- f a1 .. an
-- evaluates to e with each xi replaced by ai

-- functions can be given just some of their arguments...
foosev = foo 7
-- foosev 4
-- foo 7 4
-- 1000*7+10*4

ffo a b = foo (foo a  b)
-- ffo 7 5 3
-- foo (foo 7  5) 3

flp f x y = f y x
-- flp foo 5 3
-- flp ffo 7 5 3
-- flp (ffo 7) 5 3
-- (ffo) 7 3 5














