Skip to content

Commit

Permalink
Set 9a Ex 9: rewrite task, fix typo in test
Browse files Browse the repository at this point in the history
  • Loading branch information
opqdonut committed Aug 16, 2021
1 parent a8868f1 commit 8561b73
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
34 changes: 11 additions & 23 deletions exercises/Set9a.hs
Original file line number Diff line number Diff line change
Expand Up @@ -187,24 +187,14 @@ compose = todo
------------------------------------------------------------------------------
-- Ex 9: Reorder a list using a list of indices.
--
-- The order of elements in a list can be described using a list of
-- indices. The string "abcde" (remember that strings are lists of
-- chars) has letters 'a', 'b', 'c', 'd', and 'e' in indices 0, 1, 2,
-- 3, and 4 respectively. If we reverse the string, we get "edcba", in
-- which the five letters occur in indices 4, 3, 2, 1, and 0
-- respectively. If we swap the last two letters in "abcde", we get
-- "abced" which has the letters in indices 0, 1, 2, 4, and 3.
--
-- Thus if we encode the string "abcde" as [0,1,2,3,4], we can encode
-- "edcba", and "abced" with the index lists [4,3,2,1,0], and
-- [0,1,2,4,3] respectively.
--
-- More generally, the indices in any list xs of length n can be represented
-- as the index list [0..n-1]. If the index list is reordered, then xs can
-- be reordered accordingly. Your task is to implement the function permute
-- that performs such reordering. You may assume that the index list always
-- has the correct length and contents (no duplicates, missing or negative
-- indices or any other such funny business).
-- You are given a list of indices (numbers from 0 to n) and an input
-- list (of length n). Each index in the index list tells you where to
-- place the corresponding element from the input list in the output
-- list.
--
-- For example, if the 3rd element of the index list is 7, and the 3rd
-- element of the input list is 'a', the output list should have 'a'
-- at index 7.
--
-- (The index lists discussed in this exercise correspond to permutations in
-- math. In fact, permutations can be multiplied which is a special case of
Expand All @@ -214,16 +204,14 @@ compose = todo
-- Examples:
-- permute [0,1] [True, False] ==> [True, False]
-- permute [1,0] [True, False] ==> [False, True]
-- permute [0, 1, 2, 3, 4] "curry" ==> "curry"
-- permute [4, 3, 2, 1, 0] "curry" ==> "yrruc"
-- permute [2, 1, 0, 3, 4] "curry" ==> "rucry"
-- permute [0,1,2,3] "hask" ==> "hask"
-- permute [2,0,1,3] "hask" ==> "ashk"
-- permute [1,2,3,0] "hask" ==> "khas"
-- permute [2, 1, 0] (permute [2, 1, 0] "foo") ==> "foo"
-- permute [1, 0, 2] (permute [0, 2, 1] [9,3,5]) ==> [5,9,3]
-- permute [0, 2, 1] (permute [1, 0, 2] [9,3,5]) ==> [3,5,9]
-- permute ([0, 2, 1] `multiply` [1, 0, 2]) [9,3,5] ==> [5,9,3]
-- permute ([1, 0, 2] `multiply` [0, 2, 1]) [9,3,5] ==> [3,5,9]
--
--

-- A type alias for index lists.
type Permutation = [Int]
Expand Down
2 changes: 1 addition & 1 deletion exercises/Set9aTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,5 @@ ex9_compose = property $ do
\was " ++ show xs ++ ";\n permute (multiply p q) xs evaluated to " ++
show (permute (multiply p q) xs) ++ ",\n while \
\permute p (permute q xs) evaluated to " ++
show (permute q (permute p xs)))
show (permute p (permute q xs)))
(permute (multiply p q) xs == permute p (permute q xs))

0 comments on commit 8561b73

Please sign in to comment.