CS计算机代考程序代写 Java Haskell AI “Pascal [Java] is for building pyramids – imposing, breathtaking, static structures built by armies pushing heavy blocks into place. Lisp [Haskell] is for building organisms – imposing, breathtaking, dynamic structures built by squads fitting fluctuating myriads of simpler organisms into place.

“Pascal [Java] is for building pyramids – imposing, breathtaking, static structures built by armies pushing heavy blocks into place. Lisp [Haskell] is for building organisms – imposing, breathtaking, dynamic structures built by squads fitting fluctuating myriads of simpler organisms into place.

the pyramid must stand unchanged for a millennium; the organism must evolve or perish.”
– Alan J. Perlis, Foreword to “Structure and Interpretation of Computer Programs”, 1985, 1996
©D. Poole 2021
CPSC 312 — Lecture 11 1 / 10

Review
Haskell is a functional programming language Strongly typed, but with type inference
Bool
Num, Int, Integer, Fractional, Floating, Double Eq, Ord
Tuple, List, Function
Classes, type variables
List comprehension [f x | x<-list, cond x] foldr ⊕ v [a1,a2,..an]=a1⊕(a2⊕(...⊕(an⊕v))) foldl ⊕ v [a1,a2,..an]=(((v⊕a1)⊕a2)⊕...)⊕an reduction, call-by-value, call-by-name, lazy evaluation type defines new types in terms of other types data defines new types, constructors (and accessors and deconstructors) IO t do <- CPSC 312 — Lecture 11 2 / 10 ©D. Poole 2021 Input-Output (IOAdder.hs, IOAdder2.hs) IO t is a type for input and output type IO t = World -> (t,World) do v1 <- a1 ... vn <- an return (f v1 ... vn) Each ai is of type IO ti for some type ti vi is of type ti ai gets world from ai−1, gives value to vi and world to ai+1 When called from prompt, a1 gets world from system. I.e., the function is applied to the state of the system. Type of do expression is IO t where t is return type of f How is return defined? return v world = (v,world) which is returned as the value for do. Value of v is printed in interactive mode. CPSC 312 — Lecture 11 3 / 10 ©D. Poole 2021 Clicker Question Consider the program: foo = do putStrLn("Test in foo") return (3 :: Integer) What is the type of foo? A foo :: [Char] B foo :: IO [Char] C foo :: Integer D foo :: IO Integer See TestDo.hs ©D. Poole 2021 CPSC 312 — Lecture 11 4 / 10 Clicker Question Consider the program: foo = do putStrLn("Test in foo") return 3 What output from evaluating foo in ghci? A Test in foo 3 B3 Test in foo C3 D "Test in foo" ©D. Poole 2021 CPSC 312 — Lecture 11 5 / 10 Clicker Question foo = do putStrLn("Test in foo") return (3 :: Integer) bar = do putStrLn("Test in bar") v <- foo putStrLn ("v is "++show v) return ("v^3 is "++show (v^3)) What is the inferred type of bar? A bar :: [Char] B bar :: IO [Char] C bar :: Integer D bar :: IO Integer See TestDo.hs ©D. Poole 2021 CPSC 312 — Lecture 11 6 / 10 Clicker Question foo = do putStrLn("Test in foo") return 3 bar = do putStrLn("Test in bar") v <- foo w <- v+7 ---this line--- putStrLn ("v is "++show v) return ("v^3 is "++show (v^3)) This given an error with ---this line--- included, but not with it removed. Why? A v isnotanumberandsocannotbeaddedto7 B v+7isaNumtype,notoftypeIOt C w is not used in the rest of the definition D ---this line--- is illegal at the end of the line ©D. Poole 2021 CPSC 312 — Lecture 11 7 / 10 Clicker Question foo = do putStrLn("Test in foo") return 3 bar2 = do putStrLn("Test in bar") v <- foo w <- 7 putStrLn ("v is "++show v) return ("v^3 is "++show (v^3)) What error message does Haskell produce A No instance for (Show a0) arising from a use of ’print’ B Runtime error: ’7’ is not an IO t0 C You are not allowed to have ’w <- 7’ in a ’do’ D No instance for (Num (IO t0)) arising from the literal ’7’ E parse error (possibly incorrect indentation or mismatched brackets) ©D. Poole 2021 CPSC 312 — Lecture 11 8 / 10 Clicker Question foo = do putStrLn("Test in foo") return 3 bar3 = do putStrLn("Test in bar") v <- foo w <- v return ("v^2 is "++show (v^2)) Why does Haskell produce the error: No instance for (Num (IO t0)) arising from the literal ’3’ A Itisatypo;itshouldsayw <- viswrong B It is possible that w <- v is legal expression if v is of type (IO t0) but v must also be in the class Num C The error messages are designed to be confusing D “return 3” is illegal in foo ©D. Poole 2021 CPSC 312 — Lecture 11 9 / 10 Clicker Question Give the types and declarations which could be written as: return :: t -> IO t
return v world = (v,world)
and the program
foo = do
aaa <- return 5 return (aaa+4) This does not give a compile time error when loaded. If you type foo in at the prompt, what is returned: A5 B9 C runtime error D No instance for (Show (IO Integer)) arising from a use of ‘print’ ©D. Poole 2021 CPSC 312 — Lecture 11 10 / 10