Skip to content

Notes for Week 9 Input and Output

newmana edited this page Feb 13, 2012 · 7 revisions

Links:

Exercises:

  1. Write a program which asks the user for the base and height of a right angled triangle, calculates its area and prints it to the screen. The interaction should look something like:

The base? 3.3 The height? 5.4 The area of that triangle is 8.91

Hint: you can use the function read to convert user strings like "3.3" into numbers like 3.3 and function show to convert a number into string.

  1. Write a program that asks the user for his or her name. If the name is one of Simon, John or Phil, tell the user that you think Haskell is a great programming language. If the name is Koen, tell them that you think debugging Haskell is fun. Otherwise, tell the user that you don't know who he or she is.

Write two different versions of this program, one using if statements, the other using a case statement.

  1. Consider two versions of the same program:

a) do name <- getLine let loudName = makeLoud name putStrLn ("Hello " ++ loudName ++ "!") putStrLn ("Oh boy! Am I excited to meet you, " ++ loudName)

b) do name <- getLine let loudName = makeLoud name in do putStrLn ("Hello " ++ loudName ++ "!") putStrLn ("Oh boy! Am I excited to meet you, " ++ loudName)

Why does version b of the let binding require an extra do keyword? Do you always need the extra do?

Questions raised during the group:

Is Haskell statically linked?

Yes. To dynamically link do this ghc --make -dynamic Main.hs

More info here http://www.haskell.org/ghc/docs/6.12.1/html/users_guide/using-shared-libs.html

Answers to Exercises:

//TODO

Interesting Asides

Then operator (>>) "operator is easy to translate between do notation and plain code"

putStr "Hello" >> putStr " " >> putStr "world!" >> putStr "\n"

We can rewrite it in do notation as follows:

do putStr "Hello" putStr " " putStr "world!" putStr "\n"

http://en.wikibooks.org/wiki/Haskell/do_Notation

Clone this wiki locally