Turn on thread page Beta
    Offline

    1
    ReputationRep:
    (Original post by Alaric)
    What are you trying to do? Have everything except the last element ?
    The 2nd last element of the list. I now have function that does

    > val 'a butlast = fn : 'a list -> 'a list

    I want it to return 'a not 'a list.
    Offline

    1
    ReputationRep:
    (Original post by Alaric)
    Are there pictures?
    Yep. Unfortunately. If you sell some of your dignity to the Deviless, she might let you see how "pretty" I was...
    Offline

    1
    ReputationRep:
    I can't remember what the practical was asking for now... If it's asking the functions to return something other than 'a list then I'm in trouble. Here's my code:

    (* Q2 *)
    - fun butlast [x] = []
    | butlast ([y,x]) = [y]
    | butlast (x::rest) = butlast(rest);

    (* Q3 *)
    - fun nth (x::rest,0) = []
    | nth (x::rest, 1) = [x]
    | nth (x::rest, n) = nth(rest, n-1);

    Alaric is not answering why my previous code takes in 'a list list and returns 'a list. So, guys (Willa, Wombat, Squishy) please feel free to destroy my half destoryed ego.
    Offline

    1
    ReputationRep:
    (Original post by Camford)
    The 2nd last element of the list. I now have function that does

    > val 'a butlast = fn : 'a list -> 'a list

    I want it to return 'a not 'a list.
    of course, ML will warn you about the elegant solution:

    fun penultimate (y::x::[]) = y | penultimate (y::x::xs) = penultimate (x::xs);
    Offline

    1
    ReputationRep:
    (Original post by Alaric)
    of course, ML will warn you about the elegant solution:

    fun penultimate (y::x::[]) = y | penultimate (y::x::xs) = penultimate (x::xs);
    I got that ok... But, Dr Frank King has given out this sheet with half of the fun declarations written on it. And for this one it starts off with

    fun butlast [x] = [] !!
    Where do you go from there?

    Edit: I need to move to bed now... otherwise, I won't make it in time for lunch. *yawns* *But shudders when the thought of himself in a bra creeps through the mind.*
    Offline

    1
    ReputationRep:
    (Original post by Camford)
    I got that ok... But, Dr Frank King has given out this sheet with half of the fun declarations written on it. And for this one it starts off with

    fun butlast [x] = [] !!
    Where do you go from there?

    Edit: I need to move to bed now... otherwise, I won't make it in time for lunch. *yawns* *But shudders when the thought of himself in a bra creeps through the mind.*
    lol ok, sleep well

    I can't think it's too late at night for me to do ML either, it never did like me

    Alaric.
    Offline

    1
    ReputationRep:
    (Original post by Alaric)
    lol ok, sleep well

    I can't think it's too late at night for me to do ML either, it never did like me

    Alaric.
    You owe me the answer to Dr King's fun declaration though... I'll chase you up for it tomorrow late in the afternoon.
    Offline

    0
    ReputationRep:
    (Original post by Alaric)
    x can't be [] because then it'd be of type `a list list and not `a list. x must always be `a. [] is of type list iirc.
    Ah, that would explain the odd things that kept happening. In which case, Will's question stands.
    Offline

    0
    ReputationRep:
    I think you were right wombat, becuase as soon as i added a case for an empty list, the function became exhaustive.

    and for the butlast thing, you just have as the second line:

    | butlast(x::rest) = x :: butlast(rest);

    it calls itself and will remove the last list element.
    Offline

    0
    ReputationRep:
    (Original post by Camford)
    - fun butlast [x] = []
    | butlast (y::[x]) = y
    | butlast (y::rest) = butlast (rest);

    that returns

    > val 'a butlast = fn : 'a list list -> 'a list

    why?
    this function asks for 'a list list because of your (unnecsssary) second line. That line calles for a list, where the tail end is itself a list with a head and a tail. You dont write: y::[x]....because that means: head :: (head :: tail), hence the list list

    thje above function doesnt work anyway...it doesnt do what is asked!
    Offline

    1
    ReputationRep:
    (Original post by Willa)
    thje above function doesnt work anyway...it doesnt do what is asked!
    is it supposed to return the list apart from the last element?
    Offline

    0
    ReputationRep:
    yes, which is what the function i described above does!
    Offline

    1
    ReputationRep:
    (Original post by Willa)
    yes, which is what the function i described above does!
    indeed it does, but Camford is trying to do a penultimate function, I don't know why!

    And as a reminder to everyone, tonight is Clare Comedy - 9pm Cellars (2 or 3 pounds I think).

    Alaric.
    Offline

    0
    ReputationRep:
    ooo more comedy...excellent. I went to the medic revue last night and it was brill!
    Offline

    1
    ReputationRep:
    (Original post by Willa)
    this function asks for 'a list list because of your (unnecsssary) second line. That line calles for a list, where the tail end is itself a list with a head and a tail. You dont write: y::[x]....because that means: head :: (head :: tail), hence the list list

    thje above function doesnt work anyway...it doesnt do what is asked!
    Argh~~~~~~ My English is letting me down!!
    Offline

    20
    ReputationRep:
    (Original post by Willa)
    ooo more comedy...excellent. I went to the medic revue last night and it was brill!
    Oooh, were you there? I didn't see you! Twas indeed very good, although some material recycled from the last time I saw them.
    Offline

    1
    ReputationRep:
    I'm gonna stick this link here. Enjoy the motion.
    Offline

    0
    ReputationRep:
    (Original post by Helenia)
    Oooh, were you there? I didn't see you! Twas indeed very good, although some material recycled from the last time I saw them.
    which was your favourite sketch? The matrix one was pretty damn halarious....but i did like the gremlins one as well!
    Offline

    20
    ReputationRep:
    (Original post by Willa)
    which was your favourite sketch? The matrix one was pretty damn halarious....but i did like the gremlins one as well!
    I love the Matrix one, though I had seen it before. The gay scarecrows one was rather random but very good.
    Offline

    1
    ReputationRep:
    (Original post by Willa)
    which was your favourite sketch? The matrix one was pretty damn halarious....but i did like the gremlins one as well!
    Oh oh oh! I saw the Medic's Review as well! (but on Friday). It's the reading / writing Gremlins one that has stuck in my mind, though the one about
    Schroedinger's cat was very good as well. Having said that, i had had a bottle of wine beforehand...
 
 
 
Poll
Favourite type of bread

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.