From e78cf17a2523231ff3b5fcefc1c97e77851bc49b Mon Sep 17 00:00:00 2001 From: eduardomadeira98 Date: Fri, 23 Feb 2024 02:47:39 +0000 Subject: [PATCH] spin words example added --- examples/PSB2/annotations/spin_words.ae | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 examples/PSB2/annotations/spin_words.ae diff --git a/examples/PSB2/annotations/spin_words.ae b/examples/PSB2/annotations/spin_words.ae new file mode 100644 index 00000000..ce6fbbaf --- /dev/null +++ b/examples/PSB2/annotations/spin_words.ae @@ -0,0 +1,18 @@ +type List; + +def String_len : (i:String) -> Int = native "lambda i: len(i)"; +def String_list_to_String : (l:List) -> String = native "lambda l: ' '.join(l)"; +def map_String_String_List: (function:(a: String) -> String) -> (l: List) -> List = native "lambda f: lambda xs: map(f, xs)"; +def String_split : (i:String) -> (j:String) -> List = native "lambda i: lambda j: i.split(j)" ; +def String_reverse : (i:String) -> String = native "lambda i: i[::-1]"; + +def spin_words (phrase: String) : String { + words: List = String_split(phrase)(" "); + reversed_words: List = map_String_String_List ((\x -> if (String_len(x) >= 5) then String_reverse(x) else x):(x:String)->String) (words); + + String_list_to_String(reversed_words) + } + +def main (args:Int) : Unit { + print (spin_words("this is another test")) +}