코드

~cpp

module Slurpys
	where

import IO

main = do
	putStrLn "slurpys by thom (masterhand at gmail dot com)"


slump string = 
	if length string == 0 
		then False
		else
			if myOr [head string=='D', head string=='E']
				then checkSubSlump (tail string)
				else False

checkSubSlump string = 
	if length string == 0
		then False
		else
			if head string == 'F'
				then checkSlumpSub (tail string)
				else False

checkSlumpSub subString = 
	-- check empty string
	if length subString == 0
		then False
		else
			if head subString == 'F'
				then  checkSlumpSub (consumeLeadingChars 'F' subString)
				-- check there exists 'G' or not.
				else if head subString == 'G'
					then slumpEndCheck subString
					-- if no, check if following string is slump or not.
					else slump subString


-- check slump end condtion
slumpEndCheck lastString =
	if (length lastString) == 0
		then False
		else if myAnd [head lastString == 'G', length lastString == 1]
		--else if head lastString == 'G'
			then True
			else slump lastString

-- remove Fs after slump
consumeLeadingChars (filterChar) (toBeFiltered) = 
	if length toBeFiltered == 0
		then []
		else
			if head toBeFiltered == filterChar
				then consumeLeadingChars (filterChar) (tail toBeFiltered)
				else toBeFiltered

-- and, or function defined by thom.
myOr booleans = length (filter (\x->x/=False) booleans) > 0
myAnd booleans = length (filter (\x->x/=False) booleans) == length booleans

-- assertion list
slumpTrueAssert = [slump "DFG", slump "EFG", slump "DFFFFFG", slump "DFDFDFDFG", slump "DFEFFFFG"]
slumpFalseAssert = [slump "DFEFF", slump "EFAHG", slump "DEFG", slump "DG", slump "EFFFFDG"]


-- check for slimp
slimp string =
	if length string == 0
		then False
		-- "AH"
		else if myAnd [length string/=0, length string==2, head string=='A', tail string=="H"] -- note that type of (tail "string") is string.
			then True
		-- length ("AB" + slimp "C" > 3)
		-- length ("A" + slump "C" > 2 )
		-- "AB" + slimp + 'H' or 'A' + slump + 'H'
		else if myAnd [length string>2, head string=='A']
			then checkSubSlimp string
		else False

checkSubSlimp string =
	if length string == 0
		then False
		else if myAnd [length string>1, head string=='A', head (tail string)== 'B']
			then checkSlimpInside string
		else if myAnd [length string>0, head string=='A']
			then checkSlumpInside string
		else False

checkSlimpInside string = slimp (tearABC string)
checkSlumpInside string = slump (tearAC string)

-- tear A, B, C from "AB" + slimp + ' C'
tearABC string = reverse (tail (reverse (tail (tail string))))
-- tear A, C from 'A' + slump + ' C'
tearAC string = reverse (tail (reverse (tail string)))

-- assertion list
slimpTrueAssert = [slimp "AH", slimp "ABAHC", slimp "ABABAHCC", slimp "ADFGC", slimp "ADFFFFGC", slimp "ABAEFGCC", slimp "ADFDFGC"]
slimpFalseAssert = [slimp "ABC", slimp "ABAH", slimp "DFGC", slimp "ABABAHC", slimp "SLIMP", slimp "ADGC"]


slurpy string =
	--myOr (slurpyTest string)
	slurpyTest 2 string

slurpyTest num string = 
	if num > (length string) -1
		then False
		else if slurpyLoopTest num string
			then True
		else slurpyTest (num+1) string

-- check slimp + slump for every sequential combination with given string.
slurpyLoopTest num string = 
	if myAnd [slimp (take num string), slump (drop num string) ]
		then True
		else False

slurpyTrueAssert = [slurpy "AHDFG", slurpy "ADFGCDFFFFFG", slurpy "ABAEFGCCDFEFFFFFG"]
slurpyFalseAssert = [slurpy "AHDFGA", slurpy "DFGAH", slurpy "ABABCC"]

-- Is structured programming appropriate for functional programming language like Haskell?

사용법

  • 문제에 나온 예제 스트링을 assert 한 리스트의 출력

~cpp
Slurpys> slurpyFalseAssert
[False,False,False]
Slurpys> slurpyTrueAssert
[True,True,True]
Slurpys> slumpTrueAssert
[True,True,True,True,True]
Slurpys> slumpFalseAssert
[False,False,False,False,False]
Slurpys> slimpTrueAssert
[True,True,True,True,True,True,True]
Slurpys> slimpFalseAssert
[False,False,False,False,False,False]
  • slurpy 검증 (slump, slimp도 마찬가지)

~cpp
Slurpys> slurpy "ADFGCDFFFFFG"
True
Slurpys> slurpy "SLURPY"
False

감상

  • 함수형 언어다운 코드가 무엇인지 모르겠다. 함수형 언어에 구조적인 코드는 과연 어울리는가?
  • 삽질의 연속, syntax error 와의 끊임없는 싸움..
  • 문제에서 제시하는 Input, Output 을 구현하지 않음
  • 한글주석 안먹는다 ㅠㅠ
----
AOI, Slurpys
Retrieved from http://wiki.zeropage.org/wiki.php/Slurpys/임인택
last modified 2021-02-07 05:28:02