CS计算机代考程序代写 Haskell AI — CPSC 312 – 2019 – Lists in Haskell

— CPSC 312 – 2019 – Lists in Haskell
module Lists3 where

— To run it, try:
— ghci
— :load Lists3

— sum [a1,a2,..an] = a1+a2+…+an
mysum [] = 0
mysum (h:r) = h + mysum r

— product [a1,a2,..an] = a1*a2*…*an
myproduct [] = 1
myproduct (h:r) = h * myproduct r

— or [a1,a2,..an] is True when one the ai is True
myor [] = False
myor (h:r) = h || myor r

— append [a1,a2,..an] [b1..bm] = [a1..an,b1,..,bm]
append [] l2 = l2
append (h:r) l2 = h : append r l2

— myfoldr op v [a1,a2,..an] = a1 op (a2 op (… op (an op v)))
myfoldr f v [] = v
myfoldr f v (x:xs) = f x (myfoldr f v xs)

— sum using foldr
mysum2 = myfoldr (+) 0

— myappend [a1,a2,..,an] [b1,b2,..,bm] = [a1,a2,..,an,b1,b2,..,bm]
myappend l1 l2 = myfoldr (:) l2 l1

myappend2 = flip (myfoldr (:))

— myappend [1,2,4,6] [11,23,45,56]
l1 ++++ l2 = myfoldr (:) l2 l1
— [1,2,4,6] ++++ [11,23,45,56]

— dotprod [x1,..,xn] [y1,..,yn] = x1*y1+…+xn*yn
dotprod v1 v2 = foldr (\ (x,y) s -> x*y+s) 0
(zip v1 v2)
— dotprod [1..5] [11..15]