How to list other lists?
source link: https://www.codesd.com/item/how-to-list-other-lists.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to list other lists?
I'm trying to make a list contains several smaller lists, and double the first element of each sub-lists.Here is my code:
double_the_first::(Num a)=>[[a]]->[a]
double_the_first list = case list of
[] ->[]
[[x]] ->[x*2]
[x:xs] ->map(*2)(map(head)[x:xs])
How can I represent a list like [[2,3],[4,5]]? I know my code is wrong, but haskell does not show any error, how? It will not work when I input a list like this:
double_the_first [[2,3],[2,4],[4,3]]
*** Exception: test.hs:(3,25)-(6,51): Non-exhaustive patterns in case
Can anyone help me?
First write a function that does what you want to each element of the list. In your case, each element is itself a list.
doubleFirst :: Num a => [a] -> [a]
doubleFirst [] = []
doubleFirst (x:xs) = (2*x:xs)
Then use map
to apply that function to the list of elements:
map doubleFirst [[2,3],[2,4],[4,3]]
Related Articles
How to list stored procedures that contain more than seven join operations only in a select statement in the SQL Server database?
How to list all the bash functions exported?
How to list all Rails 3 track jobs? rake -T seems incomplete
how to list & ldquo; shared with me & rdquo; articles via new API google drive?
How to list the branches that contain a given commit on a specific repository?
How is List.Clear () implemented in C #?
OCaml: How does List.fold_left work?
Meteor: How to list installed packages
How to list the functions / symbols contained in a dynamic library in Mac OS?
git scripting: how to list all the git branches containing a commit
How to list all grants for selecting, inserting, deleting, or updating for a user
booklist: how to list all bibliography entries without citing and edit the title in html version
How to list all classes with a specific attribute in an assembly?
How to list files in a file system based on the limit: java
How to list all tags for a Docker image on a remote registry?
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK