Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 2 > Newsgroup comp.lang.ml

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 07-18-2004, 11:03 PM
Shanon Fernald
Guest
 
Posts: n/a
Default ocaml help needed: cant get return type of float

Hi, I am rather new to functional programming. I am going to try to
use it for a component of a commercial application I'm working on. I
have so far decided to focus on ocaml because it seems to be catching
fire right now, but I may end up using bigaloo scheme if I keep
running into issues.

What I'm doing to learn the language is work through the classic text,
"Structure and Interpretation of Computer Programs", converting the
scheme examples to ocaml as I go.

I have run into a problem right away. I am trying to write a really
simple Absolute function for floats.

Here is the function that I think should work:
let abs x = if x >= 0.0 then x else -x;;

Here is the error:
Characters 37-38:
let abs x = if x >= 0.0 then x else -x;;
^
This expression has type float but is here used with type int

If I do this it compiles (but gives the wrong answer):
let abs x = if x >= 0.0 then x else x;;

or if I do this it works correctly:
let abs x = if x >= 0.0 then x else 0.0 -. x;;

Now I have tried to specify that x is a float and the return type is a
float but I maybe haven't got the syntax right or something?

could someone show me what I'm doing wrong here and perhaps a correct
version of the function? I hit this again in one of the functions in a
later exercise, so I really need to figure this out before I can move
on (or just go over to scheme).

thanks for any assistance!

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 07-19-2004, 03:04 PM
Lukas Mai
Guest
 
Posts: n/a
Default Re: ocaml help needed: cant get return type of float

Shanon Fernald schrob:
[...]
> I have run into a problem right away. I am trying to write a really
> simple Absolute function for floats.


> Here is the function that I think should work:
> let abs x = if x >= 0.0 then x else -x;;


> Here is the error:
> Characters 37-38:
> let abs x = if x >= 0.0 then x else -x;;
> ^
> This expression has type float but is here used with type int


$ ocaml
# (~-);;
- : int -> int = <fun>

In other words, unary - is a function that expects and returns an int.
x is a float, so this is a type error.

[...]
> or if I do this it works correctly:
> let abs x = if x >= 0.0 then x else 0.0 -. x;;


Yes, because the type of -. is float -> float -> float.

[...]
> could someone show me what I'm doing wrong here and perhaps a correct
> version of the function? I hit this again in one of the functions in a
> later exercise, so I really need to figure this out before I can move
> on (or just go over to scheme).


let abs x = if x >= 0.0 then x else -.x ;;

All you need is "-.", the floating point version of "-".

HTH, Lukas
--
exit sub{&{$_[0]}}->(sub{$_[1]&&print("$_[1] bottle",$_[1]!=1&&s=>
" of beer on the wall,$/$_[1] bottle",$_[1]!=1&&s=>" of beer,
take one down and pass it around,$/",$_[1]-1,' bottle',$_[1]!=2&&s=>
" of beer on the wall.$/$/")&&$_[0]->($_[0],$_[1]-1)},99)

Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Corrected: Proposal: Increasing type safety with a keyword Ioannis Vranos Newsgroup comp.lang.c 9 04-01-2009 09:17 AM
Re: set with 2 databases Ian Whitlock Newsgroup comp.soft-sys.sas 0 12-09-2005 05:18 PM
Re: Type I-IV Sum of squares with empty cells baogong jiang Newsgroup comp.soft-sys.sas 0 05-19-2005 03:35 PM



All times are GMT. The time now is 12:43 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.