exer

Attribution Non-Commercial (BY-NC)

6 views

exer

Attribution Non-Commercial (BY-NC)

- Lab 5 Motorola 68000
- Itch Protocol
- 161643577-Core-Java-Meterial.pdf
- Arrays
- PERBAIKAN-PROKOM
- dd_s11_l02.pdf
- Talend_Case Study
- Predefined ABAP Types
- C Programs
- LESSION_2_PROGRAM_DATA_AND_DICTONARY_TABLE
- Computer ISCE Sample Paper
- CPPS Chapter 2[CompatibilityMode]
- Qbasic Tutorial
- Programing Test 1
- JavaMail 1.4 Changes
- eng2
- funbasicf77
- Imagen Gdi
- Lab02-DataTypes (4)
- Introduction

You are on page 1of 3

The solutions must be handed in directly before (very latest: at the beginning of ) the exercise course on Wednesday, 16.05.2012, 11:45, in lecture hall AH 2. Alternatively you can drop your solutions into a box which is located right next to Prof. Giesls oce (until the exercise course starts). Please write the names and immatriculation numbers of all (two) students on your solution. Also please staple the individual sheets! Exercises marked with V3B are only relevant for students attending the V3B (Bachelor Informatik, Master Mathematik) version of the lecture. Exercises marked with V3M are relevant for all other students. Unmarked exercises are relevant for all students. Exercises marked with a star are voluntary bonus challenge exercises with advanced diculty. The points reachable in these exercises do not count towards the total number of reachable exercise points, so you may ignore them without any disadvantage. However, the bonus points count towards your point total, so you can work on them instead of working on the (more boring) standard exercises.

(1 + 2 + 2 = 5 points)

In this exercise you should learn how to use the Haskell list comprehension syntax. a) In Haskell, there is a pre-dened function replicate :: Int -> a -> [a]. When called with the arguments n 0 and e, it returns a list of length n which contains only elements with value e. For example replicate 3 c returns the string "ccc". Implement the function repl :: Int -> a -> [a], which should do the same as the function replicate, but dene it using only one dening equation and only a list comprehension as its right-hand side. You may use the shorthand notation [a..b], which returns a list of all integers from a to b, but you may not use replicate in your implementation of repl. b) Write a function allStrings :: [Char] -> Int -> [String], such that allStrings s n returns a list of all non-empty strings consisting only of characters from the list s and which have at most length n. The order in which the strings are returned is not important. For example allStrings "ab" 2 might return ["a","aa","ab","b","ba","bb"]. You may use up to two dening equations, but one right-hand side must be a list comprehension. c) Write a function palindromes :: [String] -> [String], that gets a list of strings, and returns a list of all strings s = w1 w2 , where w1 and w2 are strings contained in and s is a palindrome (i.e., s is the same as s reversed). For example palindromes ["say", "on", "not", "to", "as"] yields ["sayas","noton","tonot"]. Use only one dening equation and only a list comprehension as its right-hand side. Hints: You may use the pre-dened function reverse :: [a] -> [a].

a) Consider the following denition of the function minsort: minsort minsort minsort where :: [ Int ] -> [ Int ] [] = [] xs = m : minsort rest m = minimum xs rest = filter (/= m ) xs

(2 + 2 = 4 points)

Functional Programming SS12 Exercise Sheet 4 (due 16.05.2012) Here, minimum is the predened function computing the minimum of a list. We now dene minimum xs = head (minsort xs). The functions minimum and filter (/= m) both have linear complexity in the length of the input list. What is the complexity of the function minimum? b) Implement the innite list rands :: [Int] of pseudo-random numbers following the linear congruential method. For that, we need parameters m (the modulus ), a (the multiplier ), and c (the increment ). Then, for any given number rn , the next pseudo-random number can be computed using the formula rn+1 = ((a rn ) + c) % m.1 Use m = 216 , a = 25713 and c = 13849 and 42 as rst random number. Furthermore, dene a function randSum :: rst k elements of rands. Int -> Int such that randSum k yields the sum of the

(4 points)

Implement a method posSublistSums :: [Int] -> [Int] such that the call posSublistSums xs returns a list res whose n-th entry is the sum of the rst n positive numbers in xs. Your solution should work in linear time (w.r.t. the length of the input list) by using a cyclic data object. Hints: Consider that the n-th element of the result res is the sum of the (n 1)-th element of res and the n-th positive number in xs. You may use the predened function zipWith: zipWith :: ( a -> b -> c ) -> [ a ] -> [ b ] -> [ c ] zipWith f ( x : xs ) ( y : ys ) = f x y : zipWith f xs ys zipWith _ _ _ = [] This function can be used to combine two lists element-wise, i.e., zipWith f [x1 , x2 , . . .] [y1 , y2 , . . .] yields the list [(f x1 y1 ), (f x2 y2 ), . . .]. Pre-dened functions such as map, foldr, sum, zipWith, filter work in linear time (w.r.t. the list length) provided that their higher-order parameters work in constant time.

(5* points)

In this exercise, we want to implement a function repMin that takes a list xs and returns a list of the same length containing only the minimal element of xs. For example, repMin [2,1,3] should yield [1,1,1]. The straighforward implementation would look like this: repMin xs = map (\ _ -> min ) xs where min = minimum xs However, this requires two iterations over the list: rst, minimum searches for the minimal element, and then the map call replaces all elements. Using lazy programming, it is possible to nd the minimum and replace all elements in one iteration. Implement a function repMin :: (Ord a) => [a] -> [a] that does this and briey explain why your solution works as described. Hints: Use a helper function repMin :: (Ord a) => [a] -> b -> (a, [b]) that takes a list xs and a replacement value r and returns a pair where the rst element is the minimal value of xs and the second element is a list of the same length as xs, but where all elements are r. Implement repMin by calling repMin such that the minimal value returned by repMin also appears as input argument to repMin.

1 Here,

% describes the modulo operation, i.e, the remainder of division of one number by another.

(2 + 4 + 3 = 9 points)

In this exercise, we will implement a small interactive arithmetic trainer. We use the following simple data type to represent the actual arithmetic problems: data ArithProblem = Add Int Int | Mul Int Int This type and several auxiliary functions are in the le TrainerBase.hs, which you can download from our website. You can use it in your solution by writing import TrainerBase at the top of your program. An example run of the complete program could look like this: How many problems do you want ? 4 What is 2*7? 14 Your answer is correct ! What is 4*9? 35 Your answer is not correct ! What is 1+9? 11 Your answer is not correct ! What is 9*5? 45 Your answer is correct ! You got 2 answers right and 2 ones wrong . a) First, implement a function askProblem :: ArithProblem -> IO Bool. Evaluating askProblem p should then print the problem and query the user for an answer. It should then read the answer and report to the user whether the result is correct. Furthermore, it should return True in case of a correct answer and False otherwise. Hints: ArithProblem is a member of the type class Show. Thus, there is a function show :: ArithProblem -> String that returns a String representation of a given problem. For example, show (Mul 2 7) returns "2*7". The function getLine :: line from the user. IO String is similar to getChar :: IO Char, but reads a complete

The function read :: (Read a) => String -> a can be used to convert a String into types that are in the type class Read. For example, let x = "42" in read x :: Int converts the String "42" into the Int 42. TrainerBase provides a function getSol :: for a given arithmetic problem. ArithProblem -> Int that computes the solution

b) Implement a function askN :: Int -> IO (Int, Int) such that askN n asks the user for solutions to n randomly generated arithmetic problems and returns the numbers of right and wrong answers. To get a randomly generated ArithProblem, you can use the function randomProblem :: IO ArithProblem from TrainerBase.hs. Use the function askProblem from a) to communicate with the user. c) Implement the function main :: IO () that asks the user for the number n of problems that should be generated, calls askN with n and then reports the total number of right and wrong answers.

- Lab 5 Motorola 68000Uploaded byAyozzet Wef
- Itch ProtocolUploaded byshantanusharma22
- 161643577-Core-Java-Meterial.pdfUploaded byrajesh ganta
- ArraysUploaded bymcainnareddy
- PERBAIKAN-PROKOMUploaded byAldi
- dd_s11_l02.pdfUploaded byMazMoh
- Talend_Case StudyUploaded byRahul Birla
- Predefined ABAP TypesUploaded byruiforever
- C ProgramsUploaded byganesh_krishna
- LESSION_2_PROGRAM_DATA_AND_DICTONARY_TABLEUploaded byapi-19917699
- Computer ISCE Sample PaperUploaded byRahul Gupta
- CPPS Chapter 2[CompatibilityMode]Uploaded byCadetMuzammil
- Qbasic TutorialUploaded byArun
- Programing Test 1Uploaded byChad Mcdonald
- JavaMail 1.4 ChangesUploaded byEliu Florez
- eng2Uploaded byAnonymous 9WNqHZ
- funbasicf77Uploaded byEmmanuel Olguin Callejas
- Imagen GdiUploaded byWillan Betancourt
- Lab02-DataTypes (4)Uploaded bys_gamal15
- IntroductionUploaded bylaila
- UPnpUploaded byHalatra
- Week2Uploaded byShenami Sathurdei
- PHPExcel Function Reference developer documentation.odtUploaded bynoel2018
- c++Uploaded byShabbir Ahmad
- C++ Introduction to Function ArrayUploaded byalip
- Visual Basic String CodingUploaded byAakanksha Uppal
- 2017Uploaded byS M Shamim শামীম
- G_Qwik_RefUploaded byDjNIAZI
- chapt02.pptUploaded bykarthickamsec
- When In-Memory Computing is Slower than Heavy Disk UsageUploaded bytvierling

- Solution2012-08-15-V3BUploaded bytanmaya1991
- Idea Presentation (Milestone 1) - TextUploaded bytanmaya1991
- Tarpana Sanskrit SsUploaded bytmn2011
- Career CounsellingUploaded bytanmaya1991
- A Formal Technique for Software Evolution Tanmaya Mahapatra 340959Uploaded bytanmaya1991
- ArticleUploaded bytanmaya1991
- Covering LetterUploaded bytanmaya1991
- Teaching SchemeUploaded bytanmaya1991
- SyllabusUploaded bytanmaya1991
- Module EnglishUploaded bytanmaya1991
- Guru Van i April 2005Uploaded bytanmaya1991
- Maa PitambaraUploaded byBharat Mishra
- Guru Wan i May June 2001Uploaded bytanmaya1991
- ishtadevata-2Uploaded bytanmaya1991
- Brief Guide Germany Aug2012Uploaded bytanmaya1991
- Aditya Hriday StotramUploaded byRitwik Yogi
- lordsivaUploaded by_moto_moto_
- e Intro TarpanamsUploaded byJagadiswari Rajamanikam
- batuka.psUploaded bytanmaya1991
- Exam2005 BlankUploaded bytanmaya1991
- V4-exam-20100319Uploaded bytanmaya1991
- Exercise 9Uploaded bytanmaya1991
- Exercise 7Uploaded bytanmaya1991
- Exercise 8Uploaded bytanmaya1991
- Exercise 6Uploaded bytanmaya1991
- Exercise 3Uploaded bytanmaya1991
- Exercise 2Uploaded bytanmaya1991
- Exercise 1Uploaded bytanmaya1991
- V3M-sol-20100224Uploaded bytanmaya1991

- Grain Pump Loop & DR.pdfUploaded bySergio Lamana
- SagarBodkhe Resume Final MEfinalyear ME10055Uploaded bySagar Bodkhe
- R_0368_RM_0606_R1200RT_01Uploaded byTaher Al Tayeb
- Heritability of lobar brain volumes in twins supports genetic models of cerebral laterality and handedness (Geschwind et al. 2002)Uploaded byapi-3695811
- Coded Inequalities Bank WbUploaded byRAGHUBALAN DURAIRAJU
- PHY13 DIAG 2Q1718.docxUploaded byMikaella Tambis
- Digital Modulation ReviewUploaded byMuhamad Fuad
- SAT Level 2 Practice Test 17Uploaded byLeonardo Florenzi
- Applied Bayesian Econometrics for Central BankersUploaded byWendeline Kibwe
- Fluid mechanics.docxUploaded byJikku Antony
- Excelvningar DerivativeUploaded byNavneet Pandey
- Aman Agarwal HoroscopeUploaded byapi-3818255
- Locking in OS Kernels for SMP SystemsUploaded byamit4g
- Memory TestingUploaded byjitu_4
- Micom Relay TestingUploaded byPadmajan Yesodharan
- Drive Train Baja SaeUploaded byRavi Kothari
- Chapter 23-Metals and MetallurgyUploaded byBalakrishna Arpula
- Automatic Boiler BlowdownUploaded byarjmandquest
- MONETARY POLICY AND MACRO-ECONOMIC INDICATORSUploaded bymutogocath86
- OptimumKinematics - Help FileUploaded byArun Gupta
- MDBUSUploaded byjvaldiviesop
- Motor StarterUploaded by7402653
- ant colony optimization algoritham and applicationUploaded byaswathykumpalath
- CM20 ServiceUploaded byKrum Kashavarov
- NTCThermistorDesignGuide07.pdfUploaded byDGG
- happiness, political orientation, and religiosityUploaded byJennifer Davis
- om0010-summer-2016Uploaded bysmu mba solved assignments
- Final Year Project Proposal _GanUploaded byMuhammad Farhan
- Pericyclic Reactionns MyUploaded byPraveen T M
- Prestressed Steel BeamUploaded byVaibhav Gaikar