主题:[讨论]几个关于函数方程编程语言h-a-s-k-e-l-l的问题
Where relevant, provide a small amount of test data for each question, and show results taken from the HUGS system (these can be
copied and pasted into the document.) Include the test data and output with each question, but comment it out so that the .hs file that
you hand in can be run. Block comments can inserted between {- and -}.
1. Redefine the following prelude functions using explicit recursion:
decide if all logical values in a list are true
and :: [Bool] -> Bool
concatenate a list of lists
concat :: [[a]] -> [a]
produce a list with n identical elements
replicate :: Int -> a -> [a]
3. In the last laboratory you used a list comprehension to define the duplicated function,
now use recursion (and pattern matching), to define same function
duplicated :: Eq a => a -> [a] -> Bool
that takes a list element and a list and returns True iff there is more than one copy of the
list element in the list. For example:
duplicated 10 [1,2,11,11] returns False,
duplicated 10 [1,2,10,11] returns False,
duplicated 10 [1,2,10,10] returns True.
4. Using recursion, and your recursive duplicated function, define the function
nodups :: Eq a => [a] -> Bool
that takes a list and returns True iff there are no duplicated elements in the list.
For example:
nodups [1,2,3,4,5] returns True
nodups [1,2,2,3,4,4] returns False
5. Freddie the Frog wants to communicate privately with his girlfriend Francine by encrypting
messages they send to one another. Frog brains are not large, so they agree on this simple
strategy: each character in the text shall be encrypted by conversion to the character “one
greater” than it.
There are 256 possible characters, which can be associated with the integers 0 to 255. The
encryption strategy will use wrap-around from 255 to 0, i.e. the character associated with 255,
when encrypted, will become the character associated with 0.
Use mapand function composition (.)to define functions encryptand decrypt that
will allow Freddie and Francine to communicate using their strategy.
encrypt :: [Char] -> [Char]
decrypt :: [Char] -> [Char]
For this problem, you will want to use two built-in Haskell functions,
toEnum :: Int -> Char and fromEnum :: Char -> Int.
toEnumwill convert an integer into a character, fromEnumwill convert a character into an
integer.
6. Without looking at the definitions from the standard prelude, define the higher-order functions
all, any, takewhile and dropwhile.
copied and pasted into the document.) Include the test data and output with each question, but comment it out so that the .hs file that
you hand in can be run. Block comments can inserted between {- and -}.
1. Redefine the following prelude functions using explicit recursion:
decide if all logical values in a list are true
and :: [Bool] -> Bool
concatenate a list of lists
concat :: [[a]] -> [a]
produce a list with n identical elements
replicate :: Int -> a -> [a]
3. In the last laboratory you used a list comprehension to define the duplicated function,
now use recursion (and pattern matching), to define same function
duplicated :: Eq a => a -> [a] -> Bool
that takes a list element and a list and returns True iff there is more than one copy of the
list element in the list. For example:
duplicated 10 [1,2,11,11] returns False,
duplicated 10 [1,2,10,11] returns False,
duplicated 10 [1,2,10,10] returns True.
4. Using recursion, and your recursive duplicated function, define the function
nodups :: Eq a => [a] -> Bool
that takes a list and returns True iff there are no duplicated elements in the list.
For example:
nodups [1,2,3,4,5] returns True
nodups [1,2,2,3,4,4] returns False
5. Freddie the Frog wants to communicate privately with his girlfriend Francine by encrypting
messages they send to one another. Frog brains are not large, so they agree on this simple
strategy: each character in the text shall be encrypted by conversion to the character “one
greater” than it.
There are 256 possible characters, which can be associated with the integers 0 to 255. The
encryption strategy will use wrap-around from 255 to 0, i.e. the character associated with 255,
when encrypted, will become the character associated with 0.
Use mapand function composition (.)to define functions encryptand decrypt that
will allow Freddie and Francine to communicate using their strategy.
encrypt :: [Char] -> [Char]
decrypt :: [Char] -> [Char]
For this problem, you will want to use two built-in Haskell functions,
toEnum :: Int -> Char and fromEnum :: Char -> Int.
toEnumwill convert an integer into a character, fromEnumwill convert a character into an
integer.
6. Without looking at the definitions from the standard prelude, define the higher-order functions
all, any, takewhile and dropwhile.