Jump to content
Kaderudo

Duvidas em exercicios de Listas em Haskell

Recommended Posts

Kaderudo

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.

Examples:

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

[1,2,3]

Main> sumListsPlus [[],[]]

[]

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

[6,9]

Exercise 3.2

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

Examples:

Main> merge [] [1,2]

[1,2]

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

[0,1,2,3,4,5]

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

[-5,-4,-3,-2,-1,0,0,1,2,3,4,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.

Examples:

Main> insert 5 [0..4]

[0,1,2,3,4,5]

Main> insert 3 [1,3,6]

[1,3,3,6]

Main> insert (-1) []

[-1]

Main> insert 6 [3,5,7]

[3,5,6,7]

Exercise 3.4

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

Examples:

Main> dup []

[]

Main> dup [3,5,2]

[3,3,5,5,2,2]

Exercise 3.5

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

Examples:

Main> oddsBefore [2]

[2]

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

[3,1,4,2]

Main> oddsBefore [10..20]

[11,13,15,17,19,10,12,14,16,18,20]

Obrigado e Bom Ano

Share this post


Link to post
Share on other sites
estrelusca

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
estrelusca

merge::[int]->[int]->[int]

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.

Guest
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.