Jump to content

Duvidas em exercicios de Listas em Haskell

Recommended Posts


Um colega meu encontrou um óptimo site com exercícios em haskell, mas quando cheguei à parte das listas parei xD

Preciso de ajuda neste cinco exercicios, vou por tal igual como ta no site:

Exercise 3.1

Write a function sumListsPlus that, given a list of integer lists all having the same length, computes the sum list resulting of adding all elements of the same position.


Main> sumListsPlus [[1,2,3]]


Main> sumListsPlus [[],[]]


Main> sumListsPlus [[2,5],[3,0],[1,4]]


Exercise 3.2

Write a function merge that, given two ordered lists, computes the ordered merge list. Please see the examples.


Main> merge [] [1,2]


Main> merge [0,2,4] [1,3,5]


Main> merge [(-5)..0] [0..5]


Exercise 3.3

Write a function insert that, given an integer and an ordered list, computes the ordered list resulting of inserting the integer in the given list in the first position that keeps the list ordered.


Main> insert 5 [0..4]


Main> insert 3 [1,3,6]


Main> insert (-1) []


Main> insert 6 [3,5,7]


Exercise 3.4

Program a function dup that replaces each element of a list by two occurrences of that element. 


Main> dup []


Main> dup [3,5,2]


Exercise 3.5

Program a function oddsBefore that put all odd elements before all even elements in a list of integers.


Main> oddsBefore [2]


Main> oddsBefore [4,2,3,1]


Main> oddsBefore [10..20]


Obrigado e Bom Ano

Share this post

Link to post
Share on other sites

insert :: Int-> [int] -> [int]

insert x xs

| null xs = x : []

|x <= head xs = x : xs

|otherwise = (head xs) : insert  x  (tail xs)

Share this post

Link to post
Share on other sites


merge xs ys

|null xs = []

|otherwise = qsort (xs ++ ys)

so consegui fazer estes dois

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.