Haskell用新书替换了给定的现有书 [英] Haskell replace a given existing book with a new book
问题描述
使用Haskell,我正在寻找一种方法来用新书替换给定的现有书.新书应有名称,页数,等级,并应以3位数字显示每周借出的次数.替换后,书籍的顺序应相同.
using Haskell I am looking for a way to replace a given existing book with a new book. The new book should have a name, pages, rating and it should show how many times it was lent each week with 3 figures. The order of the books should be the same after it has been replaced.
data Book = Book { name:: String
, pages:: Int, rating:: Float
, timesLentEveryWeek:: [Float]
} deriving (Eq,Ord,Show,Read)
testData1 :: [Book]
testData1 = [Book "Harry Potter" 374 9.7 [7, 5, 8],
Book "Percy Jackson & the Olympians" 530 9.8 [3 , 4, 2],
Book "Star Wars" 435 9.5 [9 , 7, 10]]
例如,如果我将本书哈利·波特" 374 9.7 [7,5,8] 替换为本书唐吉x德" 304 8.9 [4,6,3] 我应该得到结果:
For example, if i replace Book "Harry Potter" 374 9.7 [7, 5, 8] with Book "Don Quixote" 304 8.9 [4, 6, 3] I should get the result:
Book "Don Quixote" 304 8.9 [4, 6, 3],
Book "Percy Jackson & the Olympians" 530 9.8 [3 , 4, 2],
Book "Star Wars" 435 9.5 [9 , 7, 10]]
我试图通过添加removeBook递归函数并通过实现@Jason Whittle的想法来使用 map
函数将一个列表中的元素添加到另一个列表中来添加一本书来解决该问题.但是,我没有使用此功能的经验,并且在实现该功能时似乎遇到了问题:
I have attempted to solve the problem by adding a removeBook recursive function and adding a book by implementing @Jason Whittle 's idea to use the map
function to add elements from one list to another. However, i am not experienced with using this function and i seem to have an issue implementing it:
removeBook :: String -> [Book] -> [Book]
removeBook n (p:ps)
| n == name p = removeBook n ps
| otherwise = p : removeBook n ps
replaceBook :: Book -> Book -> [Book] -> String -> [Book]
replaceBook old new booksData oldBookName = map new removeBook oldBookName booksData
推荐答案
听起来您正在尝试弄清楚如何创建类似 replaceBook :: Book->的函数.书->[图书]->[Book]
,其中第一个参数是要替换的 Book
,第二个参数是要替换的 Book
,第三个参数是要替换的 Book
的列表.
It sounds like you’re trying to figure out how to create a function like replaceBook :: Book -> Book -> [Book] -> [Book]
, where the first argument is the Book
that you want to replace, the second argument is the Book
you want to replace it with, and the third argument is the list of Book
s in which you want the replacement to happen.
所以让我们从声明开始:
So let’s start with a declaration:
replaceBook :: Book -> Book -> [Book] -> [Book]
replaceBook old new bs = _
您需要问自己的一个设计问题是:如果要替换的 Book
多次出现,应该怎么办?为了我们的目的,我们将通过更好地定义 replaceBook
:
One design question you need to ask yourself is: what should happen if the Book
to be replaced occurs more than once? For our purposes, we’ll answer by better defining replaceBook
:
-- |Replaces all instances of the first argument with the second
-- argument in the third argument.
replaceBook :: Book -> Book -> [Book] -> [Book]
replaceBook old new bs = _
描述此功能的另一种方法是,我们有条件地将一个列表的元素映射到另一个列表.但是,此时,我们的Haskell感觉有点发麻,因为我们的函数与 map
函数基本上具有相同的类型:
Another way of describing this function is that we are conditionally mapping elements of one list to another. However, at this point, our Haskell senses are tingling, because our function has basically the same type as the map
function:
map :: (a -> b) -> [a] -> [b]
所以让我们从这里开始:
So let’s start there:
-- |Replaces all instances of the first argument with the second
-- argument in the third argument.
replaceBook :: Book -> Book -> [Book] -> [Book]
replaceBook old new bs = map _ bs
缺少 map
的参数的类型为 Book->书
.条件映射也可以描述为使用条件映射:
That missing argument to map
has type Book -> Book
. A conditional mapping can also be described as a mapping using a conditional:
-- |Replaces all instances of the first argument with the second
-- argument in the third argument.
replaceBook :: Book -> Book -> [Book] -> [Book]
replaceBook old new bs = map f bs
where f a = if a == old then new else a
但是,由于这是Haskell,我们还没有完成-没有理由将此功能限制为 Book
!
However, since this is Haskell, we’re not quite done—there’s no reason this function needs to be restricted to Book
s!
-- |Replaces all instances of the first argument with the second
-- argument in the third argument.
replaceBook :: Eq a => a -> a -> [a] -> [a]
replaceBook old new bs = map f bs
where f a = if a == old then new else a
这篇关于Haskell用新书替换了给定的现有书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!