Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 28th, 2006, 11:49 AM   #1
Sysop_fb
Newbie
 
Join Date: Oct 2005
Location: Missouri
Posts: 9
Rep Power: 0 Sysop_fb is on a distinguished road
ocaml pattern matching

Kind of confused for some reason... give the following...

type expr = Plus of expr * expr        (* means a + b *)
          | Minus of expr * expr       (* means a - b *)
          | Times of expr * expr       (* means a * b *)
	  | Divide of expr * expr      (* means a / b *)
          | Value of string            (* "x", "y", "n", etc. *)
	  ;;


let rec to_string e =
  match e with
    Plus (left, right)   -> "(" ^ (to_string left) ^ " + " ^ (to_string right) ^ ")"
  | Minus (left, right)  -> "(" ^ (to_string left) ^ " - " ^ (to_string right) ^ ")"
  | Times (left, right)  -> "(" ^ (to_string left) ^ " * " ^ (to_string right) ^ ")"
  | Divide (left, right) -> "(" ^ (to_string left) ^ " / " ^ (to_string right) ^ ")"
  | Value v -> v
  ;;

let print_expr e =
  print_endline (to_string e);;

the matching in to_string is where I'm getting confused a tad
So given this :
Times (Value "n", Plus (Value "x", Value "y"))

Okay while looking it at I think I figured it out... When that goes through the matching I was getting confused on why it called to_string again with each parameter after it found a match but I guess it does it because the parameter can be folded such as the Plus and needs to be recycled to the function.
Right?
Sysop_fb is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:17 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC