
[code]

    beki :: Int -> Int -> Int
    beki x y = x ^ y
    
    beki2 :: [String] -> [String]
    beki2 [x] = [x]
    beki2 (base : "^" : exponent : xs) = show (beki (read base) (read exponent)) : beki2 xs
    beki2 (x : xs) = x : beki2 xs
    beki2 xs = xs
    
    main = do
        print $ beki2 ["1", "+", "2", "^", "3","^", "2"]
    
[/code]

**実行結果**

[code]

    ["1","+","8","^","4"]
    
[/code]


CC BY-SA 4.0
2023-06-24T13:51:38.633
95361
2023-06-25T13:46:18.687
2023-06-24T16:34:22.113
3060
58829
question
haskell
実行結果["1","+","8","^","4"]なんですが、["1","+","512"]みたいにべき乗だけ計算した結果を返したいのですがどのようにプログラムを修正すればいいでしょうか？


[前回の回答](https://ja.stackoverflow.com/a/95332/43506)の応用で書けます。  
前回は再帰呼び出しの結果の`head`しか使いませんでしたが、今回は`tail`部(`ys`)を残すことで、べき乗以降の計算式を維持しています。

[code]

    beki :: [String] -> [String]
    beki [] = []
    beki (base:"^":xs) = let exponent:ys = beki xs in show (read base ^ read exponent):ys
    beki (x:xs) = x:beki xs
    
[/code]


CC BY-SA 4.0
2023-06-25T13:46:18.687
95370
2023-06-25T13:46:18.687
43506
95361
answer