1

I had two parameters. The first one was list, and the other was integer n. Our function formed a new list which contained the first n elements of the parameter list and suppose that n

([1,2,3,4],2 )  
  [1,2] 

How do I do that only using -> nil, ::, @ ?

1 Answer 1

1

What you describe is List.take function in SML basis library:

List.take ([1,2,3,4],2 )
[1,2]

If you want to make the function yourself:

fun take ([], _) = []
  | take (_, 0)  = []
  | take (x::xs, n) = x::take(xs, n-1)

Or to demonstrate the use of -> nil, ::, @ (the use of @ is not recommended, just for illustration purpose):

fun take (nil, _) = nil
  | take (_, 0)  = nil
  | take (x::xs, n) = [x] @ take(xs, n-1)
Sign up to request clarification or add additional context in comments.

7 Comments

of course how i do that different way ? I thought your response. I'm looking at another solution can you help me?
(nil, _) _ using of this. what does it mean ?
That is wildcard pattern; it means you don't care about its value.
_ instead of this . do i use another option ?
@pad, You properly mean the sml basis library, and not basis sml library. Your use of append (@) in that example is very bad coding style (i know he almost asked for it), however there is no need to show bad habits.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.