5

How to list other lists?

 3 years ago
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?

advertisements

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]]

Tags haskell

Related Articles

How to list stored procedures that contain more than seven join operations only in a select statement in the SQL Server database?

This question already has an answer here: How to list the stored procedures that contains more than seven join operations in SQL Server database? 2 answers I have a database that contains more than thousand stored procedures. I need to list the store

How to list all the bash functions exported?

In bash, we can export a function in the following way: fname(){ echo "Foo" } export -f fname In this case, function fname is exported. But how to list this or other exported functions? AFAIK, command export or export -p can be used for displayi

How to list all Rails 3 track jobs? rake -T seems incomplete

Possible Duplicate: Why is rake db:migrate:reset not listed in rake -T? Inside my Rails (3.1.3) application, I can run rake db:test:prepare. But when I run rake -T [db], that task (and many others I can use) does not show up. How do I get a complete

how to list & ldquo; shared with me & rdquo; articles via new API google drive?

When I look via web I have four files in "shared with me" folder: file1, file2, file3, fil4. I don`t own them (I can write/reade only). I tried get them with file.list method with parameter q = 'not (myId) in owners'. After that the results cont

How to list the branches that contain a given commit on a specific repository?

So I've read Git: How to find out on which branch tag is? and many other posts and I already know how to use: git branch --contains tag However, I still need to know how to list branches that contain a given commit (or tag) in specific repository. Ba

How is List.Clear () implemented in C #?

I assume it uses an array to implement List. How is List.Clear() implemented? Does it actually clean up the array or just make a new array for this list? public class List { private Array _array; public void Clear1 () { _array.length = 0; } public vo

OCaml: How does List.fold_left work?

I'm trying to find the average of a list of floats. let avg l = List.fold_left ( +. ) 0 l /. List.length l;; How does List.fold_left work? (Applies the first argument to a list (the third arg)... but then what is the second argument?) Toplevel return

Meteor: How to list installed packages

I'm starting with Meteor, and I'd like to know how to list the installed packages. meteor list would only list the available packages in the registry. Edit: Since meteor 0.9.0.1 meteor list lists the available packages in the project.Update: as of me

How to list the functions / symbols contained in a dynamic library in Mac OS?

How to list the functions / symbols contained inside a dynamic library in mac os ? I need to see the list of function exported by a dynamic library.Consider How to export symbols from a shared library and How do i find out what all symbols are export

git scripting: how to list all the git branches containing a commit

I can list all branches containing a certain commit using git branch --list --contains just fine. But as explained in the related question on how to list all branches, this is a porcelain command that should not be used in scripts. The latter questio

How to list all grants for selecting, inserting, deleting, or updating for a user

DB: Oracle 10g I can grant DML commands to a user for some tables: GRANT SELECT, UPDATE, DELETE, INSERT ON USER_A.TABLE_1 TO USER_B; How to list all grants (about select, update, insert, delete) that USER_B has received, and for what tables? I checke

booklist: how to list all bibliography entries without citing and edit the title in html version

I am using bookdown A Minimal Book Example. I want to make some changes in this according to my requirements. I wonder how to list all bibliography entries without citing them. I tried \nocite{*} in preamble.tex (comes with A Minimal Book Example) bu

How to list all classes with a specific attribute in an assembly?

Please help me on how to list all classes with a specific Attribute within an assembly in C#?Example for code that get all serializable types within an assembly: public IEnumerable<Type> GetTypesWithAttribute(Assembly assembly) { return assembly.Get

How to list files in a file system based on the limit: java

How to list the files available in file system with the starting number and ending number? like if there are 500 files in C:\Test\ then how to list files starting from 1 to 20 like give start number and end number based on this list the files availab

How to list all tags for a Docker image on a remote registry?

I'd like to know how to list all tags of a Docker image on a remote Docker registry (tutum in our case but I don't suppose it matters) using the CLI (preferred) or curl? Preferably without pulling all versions from the remote registry, I just want to

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK