Created
September 27, 2021 18:57
-
-
Save jarble/e5a1c85ffa76b86981c8eeb29ac40b01 to your computer and use it in GitHub Desktop.
Replacing matching sublists in Erlang
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
-module(helloworld). | |
-import(lists,[append/2]). | |
-export([start/0]). | |
replace_if_match(X) -> | |
case X of | |
[A, "equals", B | Tail ] -> | |
[[A,"==",B],replace_if_match(Tail)]; | |
[A, "is", B | Tail ] -> | |
[[A,"==",B],replace_if_match(Tail)]; | |
[A, "is","equal","to", B | Tail ] -> | |
[[A,"==",B],replace_if_match(Tail)]; | |
[Head | Tail] -> | |
[Head|replace_if_match(Tail)]; | |
[] -> [] | |
end. | |
start() -> | |
Lst1 = ["A", "is", ["1","+","1"], "and", "B","equals",["2","+","A"]], | |
io:fwrite(replace_if_match(Lst1)). % prints "A==1+1andB==2+A" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment