The Student Room Logo
This thread is closed

Cambridge Chat (previously New Cambridge Students Entry 2004)

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.
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... :redface:
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.
Reply 8843
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);
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.*
Reply 8845
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 :tongue:

Alaric.
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 :tongue:

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.
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.
Reply 8848
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.
Reply 8849
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!
Reply 8850
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?
Reply 8851
yes, which is what the function i described above does!
Reply 8852
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.
Reply 8853
ooo more comedy...excellent. I went to the medic revue last night and it was brill!
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!!
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.
I'm gonna stick this link here. Enjoy the motion.
Reply 8857
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!
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.
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...

Latest