|
|||
|
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! |
|
|
||||
|
||||
|
|
|
|||
|
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) |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |