Jump to content

Haskell help please

- - - - -

  • Please log in to reply
4 replies to this topic

#1
jakesmith6

jakesmith6

    Newbie

  • Members
  • Pip
  • 4 posts
I have a test in haskell and I can't write solution to one problem :( I tryied it in many ways, but...nothing :( This task is very important for me and for my further studying in the university, so I would appreciate very much if someone could help me...
The issue is:
I have to create a function, which takes a list [x1,x2,x3....xn] and converts it to a list [x1, xn, x2, xn-1, x3, xn-2]
Thank you boys!
Sincerely,
Jake Smith

edit:
I don't even know how to start - i tried creating list with 'let x = [1,2,3]' and winhugs hives me error, which is not described on the web - ERROR - Syntax error in expression (unexpected end of input)
I also tried without declaring functions and variables, because of the errors, using zip and reverse functions but with no result...sorry for losing your time anyway...
Have 45 minutes to go :(

Edited by Roger, 04 July 2011 - 07:14 PM.


#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
What have you got so far and what are you stuck on?

EDIT: Okay, so nothing so far.

This isn't too hard to accomplish in Haskell. All you're really doing is taking the contents of a list, splitting the list in half, reversing the second half of the list and interspersing it's elements over a new list. Placing the elements in this pattern is relatively simple with pattern matching, which is what we'll use.

myIntersperse (x:xs) (y:ys) = x : y : myIntersperse xs ys

myIntersperse []     ys     = ys

myIntersperse xs     []     = xs

myIntersperse _      _      = []

The reverse and length functions already exist, so we can get the size of the list and we can split it very easily using take and drop. Something like this:
myFunc list = let size = length list

                  halfSize = size `div` 2 + (size `mod` 2)

           -- Removed implementation!
Unfortunately I'm always shocked by how much can be done with so few lines in Haskell so I've already pretty much done what you asked for. This language is one of those where simply expressing what you're trying to do is sufficient to build a functioning program from it. I've removed the one-line implementation using take and drop to give you something you need to do, but I figure at this point it shouldn't be too hard.
Wow I changed my sig!

#3
jakesmith6

jakesmith6

    Newbie

  • Members
  • Pip
  • 4 posts
Unfortunately I'm completely stuck on this and the time flyies so fast :(
I would be so grateful if you could help me solve it :( this will become my favourite forum in the world!

#4
jakesmith6

jakesmith6

    Newbie

  • Members
  • Pip
  • 4 posts
Wow! ZekeDragon, you're amazing! Thank you very much for the help! I believe the "universe" will return you the favor :)
Your explanation is flawless but maybe I'm not prepared and it still seems a bit complicated to me and can't combine the two parts of code into one solution :(

#5
jakesmith6

jakesmith6

    Newbie

  • Members
  • Pip
  • 4 posts
I'm sorry for being so disturbing, but can you help me for the last time :( please, ZekeDragon...
Really sorry...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users