-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
14 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,21 @@ | ||
type aexp = | ||
| Const of int | ||
| Var of string | ||
| Power of (string * int) | ||
| Power of string * int | ||
| Times of aexp list | ||
| Sum of aexp list | ||
|
||
let rec map (f : 'c * 'b -> 'a) (l : 'c list) (x : string) : 'a list = | ||
match l with [] -> [] | hd :: tl -> f (hd, x) :: map f tl x | ||
let rec map f l x= | ||
match l with | ||
| [] -> [] | ||
| hd::tl -> (f (hd, x))::(map f tl x) | ||
|
||
|
||
let rec diff ((aexp : aexp), (x : string)) : aexp = | ||
match aexp with | ||
| Const n -> Const 0 | ||
| Var s -> if s = x then Const 1 else Const 0 | ||
| Power (s, n) -> | ||
if s = x then Times ([ Const n ] @ [ Power (s, n - 1) ]) else Const 0 | ||
| Times [] -> Times [] | ||
| [ Times hd; tl ] -> | ||
Sum | ||
( [ Times ([ diff (hd, x) ] @ [ tl ]) ] | ||
@ [ Times ([ hd ] @ [ diff (tl, x) ]) ] ) | ||
| Times hd :: tl -> | ||
Sum | ||
( [ Times ([ diff (hd, x) ] @ tl) ] | ||
@ [ Times ([ hd ] @ [ diff (Times tl, x) ]) ] ) | ||
| Sum lst -> Sum (map diff lst x) | ||
let rec diff : aexp * string -> aexp | ||
=fun (aexp,x) -> match aexp with | ||
Const n -> Const 0 | ||
|Var s -> if (s = x) then Const 1 else Const 0 | ||
|Power (s, n) -> if (s = x) then Times ([Const n] @ [Power (s, n-1)]) else Const 0 | ||
|Times [] -> Times [] | ||
|Times (hd::tl::[]) -> Sum ([Times ([diff (hd, x)] @ [tl])] @ [Times ([hd] @ [diff (tl, x)])]) | ||
|Times (hd::tl) -> Sum ([Times ([diff (hd, x)] @ tl)] @ [Times ([hd] @ [diff (Times tl, x)])]) | ||
|Sum lst -> Sum (map diff lst x) |