3
How do I create a function that takes a list (can be dynamic or static) and returns the n-issimo element of that list? I use the Haskell language.
3
How do I create a function that takes a list (can be dynamic or static) and returns the n-issimo element of that list? I use the Haskell language.
3
You just need to define that the lista
you’ll get a list of some guy.
Example:
lista:: [a]->[a]
lista a=a
Thank you my brother
2
Given any list xs
... you can find here the function you are looking for (it is in the item 1.2) and more.
Basic
length xs
reverse xs
1.2. Find / search
Get umpteenth element out of a list. xs !! n
(Related: head xs
returns the first list element.)
(Related: last xs
returns the last list element.)
Get a list of all elements that match any condition. filter my_test xs
(Returns everything that passes the test.)
Find the largest / smallest element in a list.
mínimo xs
máximo xs
(It works not just for numbers, but for anything that is a member doOrdclasse
. In particular, this includes characters and strings.
1.3. Adding
novo_elemento : xs
xs ++ [ new_element ]
Enter an element in the middle of a list.
Generally, you will have to split the list into two smaller lists, put the new element in the middle, and then put it all back together. For example: let ( ys, zs ) = splitAt n xs em ys ++ [ novo_elemento ] ++ zs
Join two lists together. list1 ++ list2
1.4. Excluding
Delete the first N elements from a list. drop n xs
(Related: Tail xsremoves only one element.
(Related: init xsremoves only the last element.)
take n xs
Divide a list into two smaller lists (at nth position). splitAt n xs
(Returns a tuple of two lists.)
Delete only the tenth element of a list.
This is complicated. AFAIK, there is no internal function that does this. You have to split the list in two, remove the element from a list and join them again, like this: let (ys,zs) = splitAt n xs in ys ++ (tail zs)
(Related: tail xs
removes the first element.)
(Related: init xs
removes the last element. Slow, if the list is long.)
Exclude elements that meet any condition.
Haskell has a function called filter
who will do this for you. Beware, however, it must be called 'select'. For example, filter odd xs
returns a list of odd numbers. That is, it eliminates everything that is not strange.
1.5. Testing several conditions
null xs
any my_test xs
all my_test xs
1.6. Modifying the list or its elements
map my_function xs
Apply a function only to some elements of a list.
Assuming you just want to apply the function f
for elements for which function p returns true, you can do this: map (\x -> if p x then f x else x) xs
Convert a list of Foos to a bar list.
Find or write a function to convert foo in bar and apply it to the entire list using map
.
Number the elements of a list (so I can process each one differently according to its position). zip xs [ 0 .. ]
(For example, zip ['a','b','c'] [0..]
gives [ ( 'a', 0 ) , ( 'b', 1 ) , ( 'c', 2 ) ]
.)
map ($ my_element) xs
Total a list of numbers. sun xs
(Related: product xs
will multiply all elements together instead of adding them.)
Sort a list
You will need to import Data.List
first, but you can do sort xs
.
my_element 'elem' xs
1.7. Lists and IORun a list of IO actions.
Turn an IO list into an IO action that returns a list of results: sequence xs
Prelude > sequence [putStr "ola", putStrLn "mundo"]
Olá Mundo
[(),()]
(Note: You may want to use sequencing instead, as in the case above, if your actions only return ()
)
Perform an IO action on each element of a list.
You could map the IO function to your list (resulting in a list of actions) and then run it using the above trick. But it’s much simpler to do that:
mapM my_action xs
or mapM_ my_action xs
where mapM f xs = sequence (map f xs)
and in the same way to sequence_
.
Notes on speed
Lists Haskell are common single link lists. (Look up the term in any data structure book.) This gives them certain speed properties that are worth knowing.
2.1. Quick operation The following operations are always fast:
:
operator)head
(take first element)tail
(remove the first element)
2.2. Slower operations
Any function that does something with the N-th element or the first N elements usually slows down as N increases. The next all slow down like n
gets bigger:
xs !! n
take n xs
drop n xs
splitAt n xs
Any function that needs to process the entire list obviously slows down as the list gets bigger. The following all slow down like the list xs
gets bigger:
length xs
list1 ++ list2
(the speed depends only on the size of list1)last xs
map my_fn xs
filter my_test xs
zip my_fn list1 list2
(the speed depends on the smallest of the two lists - as well as the size of the result list!)x 'elem' xs
sum xs
minimum xs
maximum xs
It takes such a long answer?
Thank you so much I was just thinking about how not to be offensive... kkkkkkk
2
The most direct in Haskell is the operator !!
lista !! n
To prevent miscarriage n
is out of bounds:
nesimo :: Int -> [a] -> Maybe a
nesimo _ [] = Nothing
nesimo 1 (x : _) = Just x
nesimo n (_ : xs) = nesimo (n - 1) xs
nesimo 3 [2,3,4,5,6]
would-be just 4
and nesimo 13 [2,3,4,5,6]
would-be nothing
I liked your suggestion I already took the penultimate, look what I did... penultimo:: [l]->l penultimo a = a !! ((length a)-2)
@Arsenio, that is the spirit: from a problem, invent new problems and new solutions!
Browser other questions tagged array haskell
You are not signed in. Login or sign up in order to post.
This function already exists ... but you have to implement?
– Gustavo Fragoso
Are you learning Haskell Zero? Stackoverflow is not the best place to start like this, there are more appropriate references: Wikibook or Lyahfgg, for example.
– luispauloml