What is wrong with my code in sml? -
i don't know why code doesn't work.
fun lookup _ [] = 0 | lookup key ((k,v)::entries) = if k = key v else (lookup key entries)
that's happened when tested in cmd.
val lookup = fn : ''a -> (''a * int) list -> int - lookup (1,[(1,2),(2,3)]); val = fn : ((int * (int * int) list) * int) list -> int
there's nothing wrong code, didn't call lookup
enough arguments. make common mistakes among beginner sml programmers coming other languages. i'll try clarify that.
first, important thing know functions in standard ml this:
all functions in standard ml take 1 argument.
you might confused @ point, because lookup
function looks if it's taking 2 arguments. kind of does, not really.
there 2 main "workarounds" (i'm using quotes because great feature of language) representing functions take multiple arguments:
1. using curried functions
if need write function which, conceptually, needs 3 arguments, you'd write this:
fun quux = fn b => fn c => (* a, b , c *)
so, quux
is:
- a function, takes argument
a
, returns - a function, takes argument
b
, returns - a function, takes argument
c
, returns - the result computed using
a
,b
,c
how call quux
? this, right?
((quux a) b) c
but function application left associative, can write this:
quux b c
we don't need parentheses "call" functions! in standard ml parentheses don't mean "call function". they're used grouping expressions when want change associativity, in mathematics: (1 + 2) * 3
.
because defining quux
above cumbersome, there's syntactic shortcut in language. instead of writing this:
fun quux = fn b => fn c => (* a, b , c *)
we can write this:
fun quux b c = (* a, b , c *)
but, they're same thing. quux
still function takes argument a
, returns new function argument b
, returns new function argument c
.
ok, 1 way of representing multi-argument functions in standard ml. it's 1 used define lookup
.
2. using tuples
another common way of representing multi-argument functions accept tuple (which may have 2 many components wish). here's above example using tuple now:
fun quux args = case args of (a,b,c) => (* a, b , c *)
how call quux
now? this:
quux (a,b,c)
notice put space between quux
, tuple. it's optional, time keep remembering function application in standard ml not represented parentheses. quux
gets called because it's been put before tuple (a,b,c)
. tuples, however, require parentheses, why you're seeing them above.
again, before, it's cumbersome define quux
this:
fun quux args = case args of (a,b,c) => (* a, b , c *)
so can use great feature of language, pattern matching in argument position, lets write this:
fun quux (a,b,c) = (* a, b , c *)
ok, can answer question.
you defined lookup
using curried function syntax:
fun lookup _ [] = 0
but "called" lookup
using tuple syntax, 1
first element of tuple , [(1,2),(2,3)]
second element.
lookup (1, [(1,2),(2,3)])
why doesn't compiler complain, though. in unfortunate case, doesn't because happens type of first argument of lookup tuple. so, you've called lookup
single argument.
what wanted this:
lookup 1 [(1,2),(2,3)]
notice i'm not defining tuple anymore.
Comments
Post a Comment