0

I am fresh on SML and doing a homework by that. "Write a function number_in_month that takes a list of dates and a month (i.e., an int) and returns how many dates in the list are in the given month."

That's what I worked out and cannot see anything wrong with it. Please help.

`

    fun number_in_month (dates: (int*int*int) list,month:int) = 
    if ((#2 (hd dates)) = month)
    then val flag=1 flag+number_in_month(tl dates, month) 
    else number_in_month((tl dates),month)`

REPL tells that: replacing VAL with EQUALOP.

sshine
  • 15,635
  • 1
  • 41
  • 66

4 Answers4

2

You can't bind variables "that way". A binding of a variable is a declaration and thus cannot be done where an expression is expected.

In this case you have to use a let-in-end expression

fun foo x = 
  let 
    val a = 42 
  in 
    a*x
  end 
Jesper.Reenberg
  • 5,944
  • 23
  • 31
1

Your problem is endless recursion. Compiler can't get out of it because independ of result if..then..else you're running your function again Try this:

fun number_in_month (dates: (int*int*int) list,month:int) = 
    if null dates
    then 0
    else if ((#2 (hd dates)) = month)
    then val flag=1 flag+number_in_month(tl dates, month) 
    else number_in_month((tl dates),month)
setrul
  • 223
  • 1
  • 2
  • 9
1

I tried to fix it by myself and that was my solution:

fun number_in_month (dias: (int*int*int) list,mes:int) = if null dias then 0 else if ((#2 (hd dias)) = mes) then let val flag = 1 + number_in_month(tl dias, mes) in flag end else number_in_month((tl dias),mes)

I hope you can also use it!

Isko
  • 11
  • 1
0

The error message from REPL is confusing, but Jesper is right that you should use let-in-end expression, if you need assignment statement in functions. That will surely get you that error resolved.

vikramsjn
  • 1,013
  • 1
  • 10
  • 21